I'm simulating a calculator and would like to know how do I take just positive inputs and no other characters (negative integer, alphabets etc.)
I've tried using 2 do while loops, one validating positive integer and another validating characters, but it seems like there cant be 2 loops for 1 input, if not it will look weird...
do{
if (invalid == true)
{
cout << "Invalid input, please enter a positive number" << endl;
}
cout << "Please enter the first number:" << endl;
cin >> num1;
cin.ignore();
invalid = true;
} while (num1 < 0);
invalid = false;
With the code above it only validates the input to be positive integer, but once I input characters such as alphabets, the program crashes. Is there any way to exclude both at the same time?