I need to be able to accept user input of the date in the format MM/DD/YYYY, exactly. I am limited exclusively to the <iostream> and <iomanip> libraries for this project.
I understand, at least partially, how this can be achieved by using the string class, but our class is not allowed to use the string library for this project.
1 2 3 4 5 6 7 8 9 10 11 12 13
void Date::Input()
{
int m = 0, d = 0, y = 0;
do
{
cout << "Please enter the date in the form MM/DD/YYYY ->";
/* I have tried quite a few things here, but my understanding of how to use
the input stream is obviously limited */
} while(!ValidDate(m,d,y));
}
I tried using a char array, but I could not find a method that worked.
thanks
edit*
For clarification, this must be achieved in a single line of input, in other words with a single tap of the return/enter key.
cin << m; will read a number. When a char that is not a part of a number is found, it will stop. In, for example, "2/5/2011" that char is /. You then have to remove it (cin.get() or cin.ignore() ) and cin << d;
Thank you sir. I had tried that, but I clearly must have overcomplicated the issue.
The following is the functional code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void Date::Input()
{
int m = 0, d = 0, y = 0;
char mmddyy[] = "MM/DD/YYYY";
do
{
cout << "Please enter the date in the form MM/DD/YYYY ->";
cin >> m;
cin.get();
cin >> d;
cin.get();
cin >> y;
} while(!Set(m,d,y));
}
Set() is a member function which returns true if the date was valid. It also sets the date if it is valid.