im using a user-inputeed int to send into a switch statement in a menu system that i am using. however if the user enters a string or char by accident my program goes into an endless loop. please can someone tell me how to guard against this?
but when the user inputs a string/char it still goes into an infinite loop, although i can make out that its still going to the top where its supposed to but then looping again n again.
any help will be greatly appreciated
//EDIT:
ive just had to take out:
1 2 3 4 5 6 7
if(!isdigit(choice))
{
cout << "Choice must be an integer" << endl;
cin >> choice;
goto top;
}
as it causes the infinite loop when i actually need to enter strings in another section of my code
you can use a 'string' variable that is used to get the inputfrom user, then check valid input with isdigit(). if valid, you can use atoi() function to get an integer from a const char* (you can use string.c_str() to get it).