I have this problem with cin, and I'm not sure what to do: Here's some of my code:
My problem is thus: when you type in any other intenger besides one or two, the default message appears. But, whenever you type in a non-intenger input, the program crashes horribly. I know that since the cin variable is an intenger, that it can only accept intengers, but how do I keep the program from crashing?Code:void Menu1() { cout << "\nSelect a command:"; for ( ; ; ) { bool exit = false; cout << "\n1 - Go Exploring"; cout << "\n2 - Exit"; cout << "\nCommand: "; int input; cin >> input; switch (input) { case 1: cout << "\nOne!"; break; case 2: exit = true; break; default: cout << "\nThat wont work." break; } if (exit) break; else continue; } }
I just want the default message to be printed once if you type in anything other than an intenger.
BTW: I know that my for ( ; ; ) could be written as while(!exit), but that's just the way I like to do my menu loops.



LinkBack URL
About LinkBacks


