I am currently coding the first bit where the program will only allow people who are 18 to continue to use it. I have written the code, with a little help from another thread, to ask the user to input their age, calculate it and display the correct message to the user; however, when the user enters a symbol or other character, the program functions incorrectly (as is expected).
I would like to be able to have it so if the user enters a symbol or character, nothing will happen & it will display a message to the user telling them ti enter a valid integer.
Here is the important part of the code I have at the moment & it is the last "else" statement which I tried to see if that would work & it doesn't
//USER INPUT OF BIRTHDATE:
SelectTextColour( clDarkCyan);
SelectBackColour( clBlack);
Gotoxy(21, 10);
cout << "Please input the date of your birth ";
Gotoxy(35, 8);
SelectTextColour( clGrey);
cin >> birthDate;
Gotoxy(37, 8);
SelectTextColour( clMagenta);
cout << "/";
Gotoxy(21, 10);
SelectTextColour( clDarkCyan);
cout << "Please input the month of your birth ";
Gotoxy(38, 8);
SelectTextColour( clGrey);
cin >> birthMonth;
Gotoxy(40, 8);
SelectTextColour( clMagenta);
cout << "/";
Gotoxy(21, 10);
SelectTextColour( clDarkCyan);
cout << "Please input the year of your birth ";
Gotoxy(41, 8);
SelectTextColour( clGrey);
cin >> birthYear;
//CALCULATING AND DISPLAYING THE USER'S AGE:
if (birthMonth >= 5)
userAge = currentYear - birthYear - 1;
else
userAge = currentYear - birthYear;
Gotoxy(21, 10);
cout << " ";
SelectTextColour( clMagenta);
Gotoxy(23, 10);
cout << "You are currently " << userAge << " years of age.";
//INFORMING THE USER WHETHER THEY ARE OLD ENOUGH OR NOT
if (userAge < 18)
{
Gotoxy(10, 15);
SelectTextColour( clGrey);
SelectBackColour( clDarkRed);
cout << "You are underage, as you have to be 18 to use this program.";
Gotoxy(28, 16);
cout << "Please do not continue.";
Gotoxy(10, 19);
cout << "The program will now exit when you press any keyboard button.";
while (! _kbhit());
}
else if (userAge >= 18)
{
Gotoxy(20, 15);
SelectTextColour( clGrey);
SelectBackColour( clDarkGreen);
cout << "You are old enough to use this program.";
Gotoxy(32, 16);
cout << "Please continue";
while (! _kbhit());
}
else
{
Gotoxy(28, 15);
SelectTextColour( clGrey);
SelectBackColour( clDarkRed);
cout << "Please enter a";
Gotoxy(28, 16);
cout << "valid integer.";
while (! _kbhit());
}
This is how I have declared my variables in the program:
int birthDate, birthMonth, birthYear; //the user's date of birth int userAge; //the user's current age
Can anybody help me please and guide me in the direction of what I am doing wrong?
Also, is it possible to only allow the input of valid dates/months/years. (E.g. the user can only input the integers '1' - '12' when asked to insert the month of their birth and integers '1900 - 2008' when asked about thier year of birth)?
Thank you in advance
cJr.
----------------
Now playing on iTunes: EMINEM - Guilty Consience (Feat. Dr. Dre)
via FoxyTunes

New Topic/Question
Reply




MultiQuote








|