stylersolve stylersolve - 3 years ago 101
C++ Question

Making sure multiple inputs are numbers (c++)

I was making a program where I asked user for a date and compared it with the current date. All the functionality was there but I couldn't seem to validate the day, month and year were numbers so entering letters crashed the program. Any ideas? (Note: the functions in the do while loop work as intended)

do // This do while loop forces the user to enter a valid date before moving on
{
cout << "Enter the lent date in the format dd/mm/yyyy: " << endl;
cin >> day1 >> buffer >> month1 >> buffer >> year1;
if(cin.fail())
{
continue;
}
}

while (!validateDateSize(day1, month1, year1) || !validateDateIntegrity(day1, month1, year1));

Answer Source

It depends on the definition of your variables. Let's assume:

int day1, month1, year1; 
char buffer;

Entering a valid date such as "12/3/2017", "12-3-2017" or even "12.3.2017" would pass the test. Entering invalid dates but with a valid format such as "125.3.2017" would fail the test, and loop to offer the next chance for a correct entry.

So what's the problem ?

But if something goes wrong in the format, for example with "12/A/2017", cin would fail at the first unexpected char (here 'A'). Your code would then continue the loop. Unfortunately, the fail status of cin will remain unchanged, causing any subsequent input to fail and your code to loop forever.

How to correct it ?

You need to clear() the error status, and also ignore() the wrong characters that caused the failure and are still in the input :

   if(cin.fail())
    {
        cin.clear();                        //reset error flags
        cin.ignore(numeric_limits<streamsize>::max(),'\n');  // and ignore characters until the next newline
        continue;
    }

Online demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download