Exception Handling
How can I make my program not terminate if a letter is entered here? I am trying to restore the input stream but it's not working.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int choice;
string str1 = "Please enter a number.";
try
{
do
{
cout<<"\nTo do this, press 1."
<<"\nTo do that, press 2."
<<"\nTo do something, press 3."
<<"\nTo do nothing, press 4."
<<"\nTo exit, press 9."<<endl;
cin>>choice;
if(!cin)
{
throw str1;
cin.clear();
cin.ignore(100, '\n');
}
switch(choice)
{
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
default:
cout<<"\nInvalid input.\n";
}
}while(choice != 9);
}
catch(string inp)
{
cout<<inp<<endl;
}
return 0;
}
|
Don't throw an exception. (remove line 23)
Doh! lol Thanks!
Topic archived. No new replies allowed.