Say I have a float variable called "varFloat" and I use cin to allow the user to input a number, how can I prevent the user from entering a letter?
I have a calculator program that breaks if a letter is entered instead of a number.
cout << "Enter num1: ";
cin >> num1;
cin.ignore();
do
{
//Select function
cout << "Enter a function ('+' '-' '*' '/'):";
getline(cin, function);
if (function == "+" || function == "-" || function == "*" || function == "/")
{
break;
}
else
{
cout << function << " IS AN INVALID FUNCTION" << endl;
}
} while (function != "+" || function != "-" || function != "*" || function != "/");
If a letter is entered for num1, the program seems to skip the getline and prints " IS AN INVALID FUNCTION" endlessly.