   [FIXED] Cin is a pain
  I think I use to be able to do this, but I guess I forgot.If I am streaming a value into an int and there is an error I want it to clear the rest of whatever is in cin before doing anything else.What ends up happening is if someone enters a letter my loop keeps on going and taking more cin input or something. If someone enters a letter I want it to stop at the cin >> option again though.int display_menu(void){int option = -1;int trys = 0;while(option < 0 || option > 6){++trys;cout << "\tMain Menu" << endl;cout << "=======================================" << endl;cout << " 1.\tAdd A New Customer" << endl;cout << " 2.\tAdd A New Product" << endl;cout << " 3.\tEnter An Order" << endl;cout << " 4.\tSearch For Customer" << endl;cout << " 5.\tSearch For Products" << endl;cout << " 6.\tSearch For Order" << endl;cout << " 0.\tExit" << endl;cout << "Choice: ";cin >> option;if(cin.fail()){cout << "Error reading input.\n" << endl;cin.clear();}if(option < 0 || option > 6)cout << "Please enter a number between 0 and 6.\n" << endl;if(trys > 10){cout << "We can't understand what you want. Sorry, good bye." << endl;return -1;}}return option;}[Edited by - Danikar on August 28, 2008 12:45:09 AM]
  After your cin >> optionsaddcin.clear();cin.ignore();:D http://www.cplusplus.com/forum/beginner/1065/   ;  Why not try putting them into a boolean instead of checking to see whether the variable changes at all, works perfectly for me. Example below (note: sloppy, but you'll understand *hopefully!*)#include <iostream>#include <string>using namespace std;int main(){// booleansbool GameOver = false;bool NewCustomer = false;// variablesint option = 0;int num_of_trys = 0;int iReturn = 0;while(GameOver == false){cout << "\tMain Menu" << endl;cout << "=======================================" << endl;cout << " 1.\tAdd A New Customer" << endl;cout << " 2.\tAdd A New Product" << endl;cout << " 3.\tEnter An Order" << endl;cout << " 4.\tSearch For Customer" << endl;cout << " 5.\tSearch For Products" << endl;cout << " 6.\tSearch For Order" << endl;cout << " 0.\tExit" << endl;cout << "Choice: ";cin >> option;switch(option){case 1:// do somethingGameOver = true;NewCustomer = true;break;case 2:// do somethingGameOver = true;break;case 3:// do somethingGameOver = true;break;case 4:// do somethingGameOver = true;break;case 5:// do somethingGameOver = true;break;case 6:// do somethingGameOver = true;break;default:// errornum_of_trys += 1;break;}if(num_of_trys == 10){cout << "We can't understand what you want. Sorry, good bye." << endl;GameOver = true;}if(GameOver == true && NewCustomer == true){cout << "New customer!" << endl;cin.get();}}cin.get();return iReturn;};Quote:Original post by scheolsAfter your cin >> optionsaddcin.clear();cin.ignore();:D http://www.cplusplus.com/forum/beginner/1065/Yeah, that doesn't work. I get the same exact result.If I enter 10 10, the option menu just comes up twice. Which it should only reappear once. If I enter a character it just keeps repeating until trys > 10 then it exits the loop.   ;  Try my version and tell me how it works out, if you need me to break it up to further explain it, I'd be more then happy to do so.EDIT: Found an error in your code. When you return, your returning your option value. So if the user put the input as 6, your returning your program at 6, when it should only be returning at 1 or 0, if it's 1, then it exits, otherwise it continues to run. And your inside a while loop and your if( something happens ) loop is returning at -1. Let me rewrite your code to explain how to do this with your code#include <iostream>#include <string>using namespace std;int display_menu(void){int option = -1;int trys = 0;int iReturn = 0;while(option < 0 && trys < 11){++trys;cout << "\tMain Menu" << endl;cout << "=======================================" << endl;cout << " 1.\tAdd A New Customer" << endl;cout << " 2.\tAdd A New Product" << endl;cout << " 3.\tEnter An Order" << endl;cout << " 4.\tSearch For Customer" << endl;cout << " 5.\tSearch For Products" << endl;cout << " 6.\tSearch For Order" << endl;cout << " 0.\tExit" << endl;cout << "Choice: ";cin >> option;if(cin.fail()){cin.clear();}if(option < 0 || option > 6 && cin.fail() != true){system("CLS");cout << "Please enter a number between 0 and 6.\n" << endl;cin.get();option = -1;}if(trys > 10){system("CLS");cout << "We can't understand what you want. Sorry, good bye." << endl;}}cin.get();return iReturn;}int main(){display_menu();return 0;} [Edited by - nooblet on August 28, 2008 12:12:22 AM];  You code has the same problem as mine. All you did was add boolean variables to the function. Here is my output when I enter asdf as the option with you code.Danikar@Danikar-PC ~/Desktop$ g++ test.cppDanikar@Danikar-PC ~/Desktop$ ./a.exe   Main Menu======================================= 1.Add A New Customer 2.Add A New Product 3.Enter An Order 4.Search For Customer 5.Search For Products 6.Search For Order 0.ExitChoice: asdf   Main Menu======================================= 1.Add A New Customer 2.Add A New Product 3.Enter An Order 4.Search For Customer 5.Search For Products 6.Search For Order 0.ExitChoice:    Main Menu======================================= 1.Add A New Customer 2.Add A New Product 3.Enter An Order 4.Search For Customer 5.Search For Products 6.Search For Order 0.ExitChoice:    Main Menu======================================= 1.Add A New Customer 2.Add A New Product 3.Enter An Order 4.Search For Customer 5.Search For Products 6.Search For Order 0.ExitChoice:    Main Menu======================================= 1.Add A New Customer 2.Add A New Product 3.Enter An Order 4.Search For Customer 5.Search For Products 6.Search For Order 0.ExitChoice:    Main Menu======================================= 1.Add A New Customer 2.Add A New Product 3.Enter An Order 4.Search For Customer 5.Search For Products 6.Search For Order 0.ExitChoice:    Main Menu======================================= 1.Add A New Customer 2.Add A New Product 3.Enter An Order 4.Search For Customer 5.Search For Products 6.Search For Order 0.ExitChoice:    Main Menu======================================= 1.Add A New Customer 2.Add A New Product 3.Enter An Order 4.Search For Customer 5.Search For Products 6.Search For Order 0.ExitChoice:    Main Menu======================================= 1.Add A New Customer 2.Add A New Product 3.Enter An Order 4.Search For Customer 5.Search For Products 6.Search For Order 0.ExitChoice:    Main Menu======================================= 1.Add A New Customer 2.Add A New Product 3.Enter An Order 4.Search For Customer 5.Search For Products 6.Search For Order 0.ExitChoice: We can't understand what you want. Sorry, good bye.;  I edited your code, try that. If it doesn't work then I don't think I could help you.   ;  cin >> int always gives you a stupid infinite loop if you enter an invalid value.Store your input in a string and use atoi. Aspirin.   ;Quote:Original post by kiwibongacin >> int always gives you a stupid infinite loop if you enter an invalid value.Store your input in a string and use atoi. Aspirin.Thanks, I didn't know that. I'll be sure to do that.Quote:EDIT: Found an error in your code. When you return, your returning your option value. So if the user put the input as 6, your returning your program at 6, when it should only be returning at 1 or 0, if it's 1, then it exits, otherwise it continues to run. And your inside a while loop and your if( something happens ) loop is returning at -1. Let me rewrite your code to explain how to do this with your codeWhat are you talking about?Look, the function returns the option value to wherever I call the function from. And I use a switch there to direct my program to where it needs to go. So, in my case, I need it to return option.If it goes over 10 times I am returning -1 and letting my main function deal with a return of -1 as an error. The return -1 is not causing my loop.Also, the only reason why this doesn't look like it is looping is because of your system("CLS") call. This isn't something I should have to make a system call in order to fix. If you take out the system calls you can see the looping is still there, hence the problem still exists. Covering the problem isn't fixing the problem.I hate to be mean when someone is trying to help me, but seriously all your doing is butchering my function without even solving the original issue.My output after removing the system calls.Danikar@Danikar-PC ~/Desktop$ ./a.exe   Main Menu======================================= 1.Add A New Customer 2.Add A New Product 3.Enter An Order 4.Search For Customer 5.Search For Products 6.Search For Order 0.ExitChoice: asdfPlease enter a number between 0 and 6.   Main Menu======================================= 1.Add A New Customer 2.Add A New Product 3.Enter An Order 4.Search For Customer 5.Search For Products 6.Search For Order 0.ExitChoice: Please enter a number between 0 and 6.   Main Menu======================================= 1.Add A New Customer 2.Add A New Product 3.Enter An Order 4.Search For Customer 5.Search For Products 6.Search For Order 0.ExitChoice: Please enter a number between 0 and 6.   Main Menu======================================= 1.Add A New Customer 2.Add A New Product 3.Enter An Order 4.Search For Customer 5.Search For Products 6.Search For Order 0.ExitChoice: Please enter a number between 0 and 6.   Main Menu======================================= 1.Add A New Customer 2.Add A New Product 3.Enter An Order 4.Search For Customer 5.Search For Products 6.Search For Order 0.ExitChoice:;This topic deals with your problem.Basically, if the user provides a bad value, that value (and anything after that value) remain in the stream. You must remove this data from the stream, or successive calls will also result in a failed state.int x;std::cin >> x;while (std::cin.fail()){std::cin.clear();std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');std::cin >> x;}// do something with xAn arguably more correct way to do this is to use std::getline and parse the returned string with a std::stringstream. std::getline() will remove an entire line (up to and including the newline characater) from the input stream, so you will never encounter a failed state or need to clear the input stream.   