| | |
Issue with getline() and cin..
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Im trying to carry out input validation on a piece of code and have come across an issue.
Basically for the 1st input I used getline for the string and then for the 2nd input I use cin for the integer. I am aware you can convert the integer from a string with getline but I wish to use the validation underneath to cover for the fact that a user may enter a string instead of the required integer.
When this code runs the second revolution of the For Loop it fires out the first two requests without waiting for user input. I suspect its the clash with getline and cin but Im unsure how to resolve except for using getline which nullifies the validation code.
Any ideas?
If you need any more clarification on my long winded, tedious problem just ask.
Cheers.
Basically for the 1st input I used getline for the string and then for the 2nd input I use cin for the integer. I am aware you can convert the integer from a string with getline but I wish to use the validation underneath to cover for the fact that a user may enter a string instead of the required integer.
When this code runs the second revolution of the For Loop it fires out the first two requests without waiting for user input. I suspect its the clash with getline and cin but Im unsure how to resolve except for using getline which nullifies the validation code.
Any ideas?
If you need any more clarification on my long winded, tedious problem just ask.
Cheers.
c++ Syntax (Toggle Plain Text)
int main () { int n,x,numb; string mystr; for (n=0; n<N_TEAMS; n++) { cout << "Enter Seeded Team League : "; getline (cin,seeded[n].stdetails.league, '\n'); cin.clear(); cout << "Enter Seeded Team League Placing : "; cin >> seeded[n].stdetails.numb; cin.ignore(numeric_limits<int>::max(), '\n'); if (!cin || cin.gcount() != 1) { cout << "Not a numeric value."; cin.ignore(numeric_limits<int>::max(), '\n'); } else { cout << "Your entered number: " << seeded[n].stdetails.numb <<endl; cin.ignore(numeric_limits<int>::max(), '\n'); } cout <<"\n"; system("Pause"); system("cls"); } for (n=0; n<N_TEAMS; n++) { cout << "League is " << seeded[n].stdetails.league << endl; cout <<"\n"; cout << "Numb is " << seeded[n].stdetails.numb << endl; system("Pause"); cout <<"\n"; } }
.........scaricamento.........
•
•
•
•
You are screwing-up your gcount() by using ignore(). Get the count first, then ignore.
Hope this helps.
I was close to figuring it as I had placed cin.clear(); after the ignore and it started to work, wasnt sure why though and youve cleared (no pun intended) that up.
Thanks again! 'Solved'
.........scaricamento.........
It would be just as easy, and permit multi-digit numbers, to input as a string and convert it...
C++ Syntax (Toggle Plain Text)
#include <algorithm> #include <cctype> #include <functional> #include <sstream> #include <string> using namespace std; int main() { string s; int n; cout << "Please enter a number> "; getline( cin, s ); if (find_if( s.begin(), s.end(), not1(ptr_fun<int,int>(isdigit)) ) != s.end()) { cout << "That's not a number.\n"; } else { stringstream( s ) >> n; cout << "You entered the number " << n << endl; } cout << "Have a nice day!\n"; return EXIT_SUCCESS; }
Last edited by Duoas; May 23rd, 2008 at 3:14 pm.
![]() |
Similar Threads
- string arrays: storing input (C++)
- While Loop Help (C++)
- Substring Error ??????? (C++)
- Not waiting for input during loop (C++)
- error with int cin (C++)
- Your code exhibits undefined and implementation-defined behavior (C++)
- String copy issue (C++)
- Woah weird issue in my program... (C++)
- Multiple inputs for char / arrays of arrays (C++)
- A little help please? thanks. (C++)
Other Threads in the C++ Forum
- Previous Thread: Sometimes consts are a pain??
- Next Thread: problem with if-else
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






