C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 30th, 2005, 02:46 PM
drayel drayel is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 21 drayel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 59 m
Reputation Power: 0
Data Stream Question

Hi Gang,
In C++, I want to query the user for a bunch of different variables. BUT, if they only hit enter (with no values typed), I want the program to keep the default value. Here is what I've tried (don't laugh too hard at my code, I'm a noob):

double ts;
cout<<"Time increment ["<<time_step<<"]: ";
ts = cin.get ();
if (ts == '\0')
time_step = ts;
cout<<time_step<<endl;

I know just a plain enter will not result in a null character, but I don't know what else to try. I've tried if (!cin.get()), then cin>>time_step. I assume a better understanding of how the data stream works in C would help, which is why I'm coming to this board!

Thanks!

Reply With Quote
  #2  
Old June 30th, 2005, 02:55 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 1 m 43 sec
Reputation Power: 4
Code:
if (ts == 10) 


If you only hit <Enter>, then a "line feed", with ASCII value 10, will stored in the variable.... thus, just check to see if the variable's value is 10, if so they may have just pressed <Enter>.... however, this obviously leaves the problem of the user legitimately entering 10 as their choice, as you'd have no way to distinguish the two...

Thus, I recommend reading their choice in as a string then converting it to a float using atof().... actually, I would recommend processing all user-input as strings then converting it as needed

hth
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips.



Reply With Quote
  #3  
Old June 30th, 2005, 03:11 PM
drayel drayel is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 21 drayel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 59 m
Reputation Power: 0
Can you convert strings to double?

Reply With Quote
  #4  
Old June 30th, 2005, 04:13 PM
drayel drayel is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 21 drayel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 59 m
Reputation Power: 0
AH-HA! I got it! Here is the solution:

char ts[256];
double n;
cout<<"Time increment ["<<time_step<<"]: ";
gets (ts);
n = atof (ts);
if (ts[0] != '\0')
time_step = n;
cout<<time_step<<endl;

Pretty cool, huh? I have to give props to B-Con and cplusplus.com

Hope you all have a good day. I know I will now!

Reply With Quote
  #5  
Old June 30th, 2005, 11:18 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 1 m 43 sec
Reputation Power: 4
hth, always feels good to solve a problem, doesn't it

Reply With Quote
  #6  
Old July 5th, 2005, 06:29 AM
anki607 anki607 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 4 anki607 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 8 m 2 sec
Reputation Power: 0
Yes strings can be converted to doubles!!!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Data Stream Question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT