Greetings,
I want to prompt the user to enter todays date in format of dd/mm/yyyy
First they enter the day (dd) i.e. 1 - 31 into integer variable dd
The validation routine is as follows:-
- Is the day value EMPTY i.e. they hit the ENTER KEY then re-prompt for day value.
- Is the day value an integer, if not then re-prompt for day value.
- If the day value is an integer ensure its between 1 - 31 if not not then re-prompt for value else continue.
Im at my wits end for such a simple problem, as I cannot for the life of me trap when the user hits return.
Knowing won't trap this I then use but having to check intially when they enter the value and then again for when they are prompting due to a fail on one of the other validation checks has become too complicated for me.
I have the following code below:-
Code:
#include <iostream>
using namespace std;
int main()
{
int dd;
bool validDay = false;
//Prompt and valdiate day entry
//Loop until date is valid
cout << "\n\tPlease enter todays date:-\n\n";
cout << "\tPlease enter day e.g. 1 - 31 :";
cin >> dd;
while(!validDay)
{
//Check if enter has been pressed
//Do IF day IS entered IS a number
if (!cin.fail())
{
//Is day value valid
if (dd >=1 && dd <=31)
{
validDay = true;
}
else
{
cout << "\n\tInvalid day, please enter day value between 1 - 31 :";
}
}
else // The user enter something other than a number, doh!
{
while (cin.fail())
{
cin.clear();
cout << "\n\tWarn: Please enter a NUMBER ONLY between 1 - 31 :";
cin.ignore(80, '\n');
cin >> dd;
}
}
}
}
Can anyone please assist? I think Im making it too complicated.
I believe I will have to check if they haven't entered a value on initial prompt and re-prompts if they entered a non-numeric value or if they entered a number <= 0 or > 31
Thanks
Rob