Ok....this code
works fine by itself (Thanks Daved), but when I implement it into my program it makes me hit enter twice when I input any integer. Aside from making me hit enter twice, the program executes like it should. Below is the code, with most of the functions taken out of it.Code:#include <iostream> #include <limits> using namespace std; int main() { int f1; cout << "Enter a number: "; while(!(cin >> f1) || cin.get() != '\n') { cin.clear(); cin.ignore(std::numeric_limits<streamsize>::max(), '\n'); cout << "Error, invalid input.\nPlease re-enter: "; } }
Can anyone help me out with why this is happening?Code:#include <iostream> #include <limits> using namespace std; void VerifyInteger(int &); inline void ClearCin(); int main() { int Selection; bool TryAgain; cout << endl << "1. Choice One" << endl; cout << endl << "2. Choice Two" << endl; cout << endl << "3. Choice Three" << endl; cout << endl << "Please enter a selection: "; do { VerifyInteger(Selection); if(Selection > 0 && Selection < 4) { TryAgain = false; switch(Selection) { case 1: // Perform first function break; case 2: // Perform second function break; case 3: // Perform third function break; } } else { ClearCin(); cout << "Error\nPlease re-enter a valid selection: "; TryAgain = true; } }while(TryAgain == True); return 0; } void VerifyInteger(int &num) { while(!(cin >> num) || cin.get() != '\n') { ClearCin(); cout << "Error\nPlease re-enter a valid selection: "; } } inline void ClearCin() { cin.clear(); cin.ignore(std::numeric_limits<streamsize>::max(), '\n'); }



LinkBack URL
About LinkBacks


