| | |
If/Else Input line skipped
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 9
Reputation:
Solved Threads: 1
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.
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)
/*Write a program that reads a series of numbers (doubles) from the user, then prints the mean and the range. Notes: • You do not know ahead of time how many numbers will be in the list. • When you want to stop entering numbers, enter control+Z • The range is the difference between the lowest and the highest number. • The numbers will be in the range of 0.0 to 100.0. Ignore any numbers outside of this range.*/ #include <iostream> using namespace std; int main () { double x; double max=0.0; double min=100.0; double count = 0.0; double sum = 0.0; while(cin>>x) { cin>>x; if (x>=0.0 && x<= 100.0) { if(x>max) { max=x; } if (x<min) { min=x; } count++; sum = x + sum; } else { cout<<"out of range; ignored."<<endl; } } cout<<"max= "<<max<<endl; cout<<"min= "<<min<<endl; cout<<"count= "<<count<<endl; cout<<"sum= "<<sum<<endl; double avg = (sum)/count; double range = max - min; cout<<"The average is "<<avg<<endl; cout<<"The range is "<<range<<endl; }
1
#3 Oct 15th, 2009
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.
0
#5 Oct 16th, 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!
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.
![]() |
Similar Threads
- user input and arrays (Visual Basic 4 / 5 / 6)
- getting input from the user (C)
- Noobie help :P (C++)
- CIN Input count question? (C++)
- Record Files (Pascal and Delphi)
- checking for a space bar input (C++)
Other Threads in the C++ Forum
- Previous Thread: gmp dll in Borland c++ builder 6
- Next Thread: School project, pls help me T_T
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets





