I am writing a program that gathers user input. The program is using a structure to store the user's inputted data and is comprised of a few strings and one double. I am able to check if the user doesn't give any input for the strings (ie. an empty string) and prompt the user to try again. With the double, however, I am having some trouble.
I need to make sure the user doesn't enter an empty value, a string, or a negative value for the double. My code works when checking for a negative number, but when the user hits enter as if giving no input (empty) the program skips a line. I understand the compiler is simply waiting for user input, but it should instead check for an empty value. Here is my input validation code below:
1 2 3 4 5 6 7 8 9 10
while ((newInput.inputNum < 0) || cin.fail())
{
if(cin.fail())
{ cin.clear(); cin.ignore(); }
cout << "This must be a non-negative number." << endl;
cout << "Enter number: ";
cin >> newInput.inputNum;
}
cout << endl << endl;
I am considering creating a function to convert the double into a string and check if it is empty. I am unsure if that is the most efficient way of resolving the issue. But I am currently exploring that as a solution.
> I understand the compiler is simply waiting for user input
>> I am considering creating a function to convert the double into a string and
>> check if it is empty
your program would still wait for user input and never execute the double2string(number).empty()