If/Else Input line skipped

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 9
Reputation: calypso&noname is an unknown quantity at this point 
Solved Threads: 1
calypso&noname calypso&noname is offline Offline
Newbie Poster

If/Else Input line skipped

 
1
  #1
Oct 15th, 2009
Hi again and thanks for the help!

PROBLEM: Only every other number entered by the user is registered by my program, except the last number, which is always registered. How can I get the program to register every line? And what is causing it to skip an input line in the first place?

I can't explain it, and I've tried a number of things from moving around where the cin>>x; input is located to using cin.ignore() ; and getlines. It hasn't worked.

  1. /*Write a program that reads a series of numbers (doubles) from the user,
  2. then prints the mean and the range.
  3. Notes:
  4. • You do not know ahead of time how many numbers will be in the list.
  5. • When you want to stop entering numbers, enter control+Z
  6. • The range is the difference between the lowest and the highest number.
  7. • The numbers will be in the range of 0.0 to 100.0. Ignore any numbers outside of this range.*/
  8.  
  9. #include <iostream>
  10.  
  11. using namespace std;
  12.  
  13. int main ()
  14. {
  15. double x;
  16. double max=0.0;
  17. double min=100.0;
  18. double count = 0.0;
  19. double sum = 0.0;
  20.  
  21. while(cin>>x)
  22. {
  23. cin>>x;
  24.  
  25. if (x>=0.0 && x<= 100.0)
  26. {
  27. if(x>max)
  28. {
  29. max=x;
  30. }
  31. if (x<min)
  32. {
  33. min=x;
  34. }
  35. count++;
  36. sum = x + sum;
  37. }
  38. else
  39. {
  40. cout<<"out of range; ignored."<<endl;
  41. }
  42. }
  43.  
  44. cout<<"max= "<<max<<endl;
  45. cout<<"min= "<<min<<endl;
  46. cout<<"count= "<<count<<endl;
  47. cout<<"sum= "<<sum<<endl;
  48.  
  49.  
  50. double avg = (sum)/count;
  51. double range = max - min;
  52.  
  53. cout<<"The average is "<<avg<<endl;
  54. cout<<"The range is "<<range<<endl;
  55. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz
 
1
  #2
Oct 15th, 2009
You seem to have two cin>>x in your code at lines 23 and 21. I would delete line 23.

I might make count and integer but that is minor.
you have remembered to initialize your variables which is good.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 65
Reputation: neithan is an unknown quantity at this point 
Solved Threads: 2
neithan's Avatar
neithan neithan is offline Offline
Junior Poster in Training
 
1
  #3
Oct 15th, 2009
Originally Posted by calypso&noname View Post
  1. while(cin>>x)
  2. {
  3. cin>>x;
while(cin>>x) does TWO functions. One is to evaluate if the cin is good and another is actually executing that cin >> x so only by typing that while you already got the user input so the second cin has to be deleted.
Last edited by neithan; Oct 15th, 2009 at 7:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 9
Reputation: calypso&noname is an unknown quantity at this point 
Solved Threads: 1
calypso&noname calypso&noname is offline Offline
Newbie Poster
 
0
  #4
Oct 15th, 2009
Neithan and StuXYZ; thanks to the both of you. The extra cin>>x making it skip makes sense. I'll take that out ASAP. Thanks Neithan especially for the extra info about how the 'while' function worked. I didn't realize the while would actually execute the cin>>x.

Thanks guys!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 65
Reputation: neithan is an unknown quantity at this point 
Solved Threads: 2
neithan's Avatar
neithan neithan is offline Offline
Junior Poster in Training
 
0
  #5
Oct 16th, 2009
Originally Posted by calypso&noname View Post
Neithan and StuXYZ; thanks to the both of you. The extra cin>>x making it skip makes sense. I'll take that out ASAP. Thanks Neithan especially for the extra info about how the 'while' function worked. I didn't realize the while would actually execute the cin>>x.

Thanks guys!

I explained to you that way because i had the same understanding problem and is something that had been a pain in the ass till somebody told me that and made me happy lol.

It's the same thing as for example if (variable++ == blah)... you know, it compares if adding one to variable is equal to blah...but it's actually changin variable's value to plus one! So that is similar to while and also gave me headaches till i discovered it.

So i hope i saved you some pain in the ass! hahah
Last edited by neithan; Oct 16th, 2009 at 8:36 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 78
Reputation: drjay1627 is an unknown quantity at this point 
Solved Threads: 1
drjay1627 drjay1627 is offline Offline
Junior Poster in Training
 
0
  #6
Oct 16th, 2009
Just out of curiosity, how do you stop the while loop with control+z?

--drjay
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 78
Reputation: drjay1627 is an unknown quantity at this point 
Solved Threads: 1
drjay1627 drjay1627 is offline Offline
Junior Poster in Training
 
0
  #7
Oct 16th, 2009
Actually I figured it out. ctrl+d in linux and ctrl+z in windows!
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC