I am trying to create a calculator program and want to make sure a character is not accepted (e.g g) and the user is asked to input another number. Here is the code i am using inside the SWITCH. How do I make it so the menu will only accept numbers and restart if a number is not entered?
Num1 and num2 are doubles and the cases are char.
case '1' :cout << "Input first number: ";
cin >> num1;
I think if you just remove the semicolons after while () and add cin.clear() and cin.ignore() in your while loops as shown below, it should work. but again i'm not sure
1 2 3 4 5 6 7 8
while (!cin) // remove the semicolon
{
cin.clear(); //add this statement to clear the error status.
cin.ignore(256,'\n');
cout << "Input first number: ";
cin >> num1;
cout << "\n";
}