944,214 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 842
  • C++ RSS
Oct 15th, 2009
1

If/Else Input line skipped

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
calypso&noname is offline Offline
11 posts
since Oct 2009
Oct 15th, 2009
1
Re: If/Else Input line skipped
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.
Reputation Points: 749
Solved Threads: 135
Practically a Master Poster
StuXYZ is online now Online
661 posts
since Nov 2008
Oct 15th, 2009
1
Re: If/Else Input line skipped
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 12
Solved Threads: 2
Junior Poster in Training
neithan is offline Offline
75 posts
since Oct 2009
Oct 15th, 2009
0
Re: If/Else Input line skipped
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!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
calypso&noname is offline Offline
11 posts
since Oct 2009
Oct 16th, 2009
0
Re: If/Else Input line skipped
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.
Reputation Points: 12
Solved Threads: 2
Junior Poster in Training
neithan is offline Offline
75 posts
since Oct 2009
Oct 16th, 2009
0
Re: If/Else Input line skipped
Just out of curiosity, how do you stop the while loop with control+z?

--drjay
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
drjay1627 is offline Offline
87 posts
since Nov 2008
Oct 16th, 2009
0
Re: If/Else Input line skipped
Actually I figured it out. ctrl+d in linux and ctrl+z in windows!
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
drjay1627 is offline Offline
87 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: gmp dll in Borland c++ builder 6
Next Thread in C++ Forum Timeline: School project, pls help me T_T





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC