Hello everybody!
My program which is able to calculate your age, has been updated!
See http://www.daniweb.com/software-development/cpp/threads/432997/age-calculator for the old version.
The programs asks your birthdate and the current date. With that information, the program is able to calculate your age. It calculates the years, months and days.
This program is originally written in Dutch, so you can still find Dutch words like "jaar", "maand" and "dag", but don't worry. You won't see those words if you are running the program.
Now, my program won't you allow to enter invalid data. The program won't accept characters and symbols if values are required.
Here is the source code:
#include <iostream>
using namespace std;
int bjaar, bmaand, bdag, hjaar, hmaand, hdag, ljaar, lmaand, ldag, tjaar, tmaand, tdag;
char vraag1;
void wrongbirthyear()
{
cout << endl << "Enter a valid value for your birthyear: ";
cin.clear();
cin.ignore();
cin >> bjaar;
}
void wrongbirthmonth()
{
cout << endl << "Enter a valid value for your birthmonth: ";
cin.clear();
cin.ignore();
cin >> bmaand;
}
void wrongbirthday()
{
cout << endl << "Enter a valid value for your birthday: ";
cin.clear();
cin.ignore();
cin >> bdag;
}
void wrongcurrentyear()
{
cout << endl << "Enter a valid value for the current year: ";
cin.clear();
cin.ignore();
cin >> hjaar;
}
void wrongcurrentmonth()
{
cout << endl << "Enter a valid value for the current month: ";
cin.clear();
cin.ignore();
cin >> hmaand;
}
void wrongcurrentday()
{
cout << endl << "Enter a valid value for the current day: ";
cin.clear();
cin.ignore();
cin >> hdag;
}
int main()
{
system("CLS");
cout << "\n\n\t\t Mark Wierings's Age Calculator \n\n";
cout << "In which year were you born? (Example: 1995) ";
cin >> bjaar;
while(cin.fail())
{
wrongbirthyear();
}
cout << endl << "In which month were you born? " << endl << endl;
cout << " 1) January \n 2) February \n 3) March \n 4) April \n 5) May \n 6) June \n 7) July";
cout << endl << " 8) August \n 9) September \n 10) October \n 11) November \n 12) December";
cout << endl << endl << "Enter a value: ";
cin >> bmaand;
while(cin.fail())
{
wrongbirthmonth();
}
while(bmaand <= 0 || bmaand > 12)
{
system("CLS");
cout << endl << bmaand << " is an invalid value. ";
cout << "1 to 12 are valid values." << endl;
cout << "Enter a valid value: ";
cin >> bmaand;
while(cin.fail())
{
wrongbirthmonth();
}
}
cout << endl << "On which day of the month were you born? ";
cin >> bdag;
while(cin.fail())
{
wrongbirthday();
}
if(bmaand == 1 || bmaand == 3 || bmaand == 5 || bmaand == 7 || bmaand == 8 || bmaand == 10 || bmaand == 12)
{
while(bdag <= 0 || bdag > 31)
{
system("CLS");
cout << endl << bdag << " is an invalid value for a day of a month." << endl;
cout << "Enter a valid value. 1 to 31 are valid values: ";
cin >> bdag;
while(cin.fail())
{
wrongbirthday();
}
}
}
if(bmaand == 4 || bmaand == 6 || bmaand == 9 || bmaand == 11)
{
while(bdag <= 0 || bdag > 30)
{
system("CLS");
cout << endl << bdag << " is an invalid value for a day of a month." << endl;
cout << "Enter a valid value. 1 to 30 are valid values: ";
cin >> bdag;
while(cin.fail())
{
wrongbirthday();
}
}
}
if(bmaand == 2)
{
if(bjaar%4 == 0)
{
if(bjaar%100 == 0 && bjaar%400 != 0)
{
while(bdag <= 0 || bdag > 28)
{
system("CLS");
cout << endl << bdag << " is an invalid value for a day of a month." << endl;
cout << "Enter a valid value. 1 to 28 are valid values: ";
cin >> bdag;
while(cin.fail())
{
wrongbirthday();
}
}
}
else
{
while(bdag <= 0 || bdag > 29)
{
system("CLS");
cout << endl << bdag << " is an invalid value for a day of a month." << endl;
cout << "Enter a valid value. 1 to 29 are valid values: ";
cin >> bdag;
while(cin.fail())
{
wrongbirthday();
}
}
}
}
else
{
while(bdag <= 0 || bdag > 28)
{
system("CLS");
cout << endl << bdag << " is an invalid value for a day of a month." << endl;
cout << "Enter a valid value. 1 to 28 are valid values: ";
cin >> bdag;
while(cin.fail())
{
wrongbirthday();
}
}
}
}
if(bmaand == 1)
{
system("CLS");
cout << endl << "You were born on the " << bdag << "th January " << bjaar << "." << endl;
cout << endl << "Is that right? (Y/N) ";
cin >> vraag1;
if(vraag1 != 'Y' && vraag1 != 'y')
{
cout << endl << "The program will be restarted so you can enter your ";
cout << "data again." << endl << endl;
system("pause");
main();
}
}
if(bmaand == 2)
{
system("CLS");
cout << endl << "You were born on the " << bdag << "th February " << bjaar << "." << endl;
cout << endl << "Is that right? (Y/N) ";
cin >> vraag1;
if(vraag1 != 'Y' && vraag1 != 'y')
{
cout << endl << "The program will be restarted so you can enter your ";
cout << "data again." << endl << endl;
system("pause");
main();
}
}
if(bmaand == 3)
{
system("CLS");
cout << endl << "You were born on the " << bdag << "th March " << bjaar << "." << endl;
cout << endl << "Is that right? (Y/N) ";
cin >> vraag1;
if(vraag1 != 'Y' && vraag1 != 'y')
{
cout << endl << "The program will be restarted so you can enter your ";
cout << "data again." << endl << endl;
system("pause");
main();
}
}
if(bmaand == 4)
{
system("CLS");
cout << endl << "You were born on the " << bdag << "th April " << bjaar << "." << endl;
cout << endl << "Is that right? (Y/N) ";
cin >> vraag1;
if(vraag1 != 'Y' && vraag1 != 'y')
{
cout << endl << "The program will be restarted so you can enter your ";
cout << "data again." << endl << endl;
system("pause");
main();
}
}
if(bmaand == 5)
{
system("CLS");
cout << endl << "You were born on the " << bdag << "th May " << bjaar << "." << endl;
cout << endl << "Is that right? (Y/N) ";
cin >> vraag1;
if(vraag1 != 'Y' && vraag1 != 'y')
{
cout << endl << "The program will be restarted so you can enter your ";
cout << "data again." << endl << endl;
system("pause");
main();
}
}
if(bmaand == 6)
{
system("CLS");
cout << endl << "You were born on the " << bdag << "th June " << bjaar << "." << endl;
cout << endl << "Is that right? (Y/N) ";
cin >> vraag1;
if(vraag1 != 'Y' && vraag1 != 'y')
{
cout << endl << "The program will be restarted so you can enter your ";
cout << "data again." << endl << endl;
system("pause");
main();
}
}
if(bmaand == 7)
{
system("CLS");
cout << endl << "You were born on the " << bdag << "th July " << bjaar << "." << endl;
cout << endl << "Is that right? (Y/N) ";
cin >> vraag1;
if(vraag1 != 'Y' && vraag1 != 'y')
{
cout << endl << "The program will be restarted so you can enter your ";
cout << "data again." << endl << endl;
system("pause");
main();
}
}
if(bmaand == 8)
{
system("CLS");
cout << endl << "You were born on the " << bdag << "th August " << bjaar << "." << endl;
cout << endl << "Is that right? (Y/N) ";
cin >> vraag1;
if(vraag1 != 'Y' && vraag1 != 'y')
{
cout << endl << "The program will be restarted so you can enter your ";
cout << "data again." << endl << endl;
system("pause");
main();
}
}
if(bmaand == 9)
{
system("CLS");
cout << endl << "You were born on the " << bdag << "th September " << bjaar << "." << endl;
cout << endl << "Is that right? (Y/N) ";
cin >> vraag1;
if(vraag1 != 'Y' && vraag1 != 'y')
{
cout << endl << "The program will be restarted so you can enter your ";
cout << "data again." << endl << endl;
system("pause");
main();
}
}
if(bmaand == 10)
{
system("CLS");
cout << endl << "You were born on the " << bdag << "th October " << bjaar << "." << endl;
cout << endl << "Is that right? (Y/N) ";
cin >> vraag1;
if(vraag1 != 'Y' && vraag1 != 'y')
{
cout << endl << "The program will be restarted so you can enter your ";
cout << "data again." << endl << endl;
system("pause");
main();
}
}
if(bmaand == 11)
{
system("CLS");
cout << endl << "You were born on the " << bdag << "th November " << bjaar << "." << endl;
cout << endl << "Is that right? (Y/N) ";
cin >> vraag1;
if(vraag1 != 'Y' && vraag1 != 'y')
{
cout << endl << "The program will be restarted so you can enter your ";
cout << "data again." << endl << endl;
system("pause");
main();
}
}
if(bmaand == 12)
{
system("CLS");
cout << endl << "You were born on the " << bdag << "th December " << bjaar << "." << endl;
cout << endl << "Is that right? (Y/N) ";
cin >> vraag1;
if(vraag1 != 'Y' && vraag1 != 'y')
{
cout << endl << "The program will be restarted so you can enter your ";
cout << "data again." << endl << endl;
system("pause");
main();
}
}
system("CLS");
cout << endl << "Which year is it now? ";
cin >> hjaar;
while(cin.fail())
{
wrongcurrentyear();
}
while(hjaar < bjaar)
{
cout << endl << "Your birthyear can't be bigger than the current year." << endl;
cout << "Enter a valid year: ";
cin >> hjaar;
while(cin.fail())
{
wrongcurrentyear();
}
}
cout << endl << "Which month is it now? " << endl << endl;
cout << " 1) January \n 2) February \n 3) March \n 4) April \n 5) May \n 6) June \n 7) July";
cout << endl << " 8) August \n 9) September \n 10) October \n 11) November \n 12) December";
cout << endl << endl << "Enter a value: ";
cin >> hmaand;
while(cin.fail())
{
wrongcurrentmonth();
}
while(hmaand <= 0 || hmaand > 12)
{
system("CLS");
cout << endl << hmaand << " is an invalid value. ";
cout << "1 to 12 are valid values." << endl;
cout << "Enter a valid value: ";
cin >> hmaand;
while(cin.fail())
{
wrongcurrentmonth();
}
}
while(hjaar == bjaar && hmaand < bmaand)
{
system("CLS");
cout << endl << "You aren't born yet. This program is only intended for the people who are born" << endl;
cout << "Just wait a few months, or enter a valid current month: ";
cin >> hmaand;
while(cin.fail())
{
wrongcurrentmonth();
}
}
cout << endl << "Which day of the month is it now? ";
cin >> hdag;
while(cin.fail())
{
wrongcurrentday();
}
if(hmaand == 1 || hmaand == 3 || hmaand == 5 || hmaand == 7 || hmaand == 8 || hmaand == 10 || hmaand == 12)
{
while(hdag <= 0 || hdag > 31)
{
system("CLS");
cout << endl << hdag << " is an invalid value for a day of the month." << endl;
cout << "1 to 31 are valid values. Enter a valid value: ";
cin >> hdag;
while(cin.fail())
{
wrongcurrentday();
}
}
}
if(hmaand == 4 || hmaand == 6 || hmaand == 9 || hmaand == 11)
{
while(hdag <= 0 || hdag > 30)
{
system("CLS");
cout << endl << hdag << " is an invalid value for a day of the month." << endl;
cout << "1 to 30 are valid values. Enter a valid value: ";
cin >> hdag;
while(cin.fail())
{
wrongcurrentday();
}
}
}
if(hmaand == 2)
{
if(hjaar%4 == 0)
{
if(hjaar%100 == 0 && hjaar%400 != 0)
{
while(hdag <= 0 || hdag > 28)
{
system("CLS");
cout << endl << hdag << " is an invalid value for a day of the month." << endl;
cout << "1 to 28 are valid values. Enter a valid value: ";
cin >> hdag;
while(cin.fail())
{
wrongcurrentday();
}
}
}
else
{
while(hdag <= 0 || hdag > 29)
{
system("CLS");
cout << endl << hdag << " is an invalid value for a day of the month." << endl;
cout << "1 to 29 are valid values. Enter a valid value: ";
cin >> hdag;
while(cin.fail())
{
wrongcurrentday();
}
}
}
}
else
{
while(hdag <= 0 || hdag > 28)
{
system("CLS");
cout << endl << hdag << " is an invalid value for a day of the month." << endl;
cout << "1 to 28 are valid values. Enter a valid value: ";
cin >> hdag;
while(cin.fail())
{
wrongcurrentday();
}
}
}
}
if(hjaar == bjaar && hmaand == bmaand && hdag < bdag)
{
system("CLS");
cout << endl << "You aren't born yet. This program is only intended for the people who are born." << endl;
cout << "Just wait a few days, or enter a valid day: ";
cin >> hdag;
while(cin.fail())
{
wrongcurrentday();
}
}
cout << endl << "The system is calculating your age..." << endl << endl;
ljaar = hjaar - bjaar;
lmaand = hmaand - bmaand;
ldag = hdag - bdag;
if(lmaand < 0)
{
ljaar = ljaar - 1;
lmaand = 12 + lmaand;
}
if(ldag < 0 && lmaand == 0)
{
ljaar = ljaar - 1;
lmaand = 12 - 1;
if(hmaand == 1 || hmaand == 3 || hmaand == 5 || hmaand == 7 || hmaand == 8 || hmaand == 10 || hmaand == 12)
{
ldag = 31 + ldag;
}
if(hmaand == 4 || hmaand == 6 || hmaand == 9 || hmaand == 11)
{
ldag = 30 + ldag;
}
if(hmaand == 2)
{
ldag = 28 + ldag;
if(hjaar%4 == 0)
{
ldag = 29 + ldag;
if(hjaar%100 == 0 && hjaar%400 != 0)
{
ldag = 28 + ldag;
}
}
}
}
if(ldag < 0 && lmaand != 0)
{
lmaand = lmaand - 1;
if(hmaand == 1 || hmaand == 3 || hmaand == 5 || hmaand == 7 || hmaand == 8 || hmaand == 10 || hmaand == 12)
{
ldag = 32 + ldag;
}
if(hmaand == 4 || hmaand == 6 || hmaand == 9 || hmaand == 11)
{
ldag = 31 + ldag;
}
if(hmaand == 2)
{
ldag = 29 + ldag;
if(hjaar%4 == 0)
{
ldag = 30 + ldag;
if(hjaar%100 == 0 && hjaar%400 != 0)
{
ldag = 29 + ldag;
}
}
}
}
cout << "Birthyear: " << bjaar << endl;
cout << "Birthmonth: " << bmaand << endl;
cout << "Birthday: " << bdag << endl << endl;
if(ldag != 1 && lmaand != 1)
{
cout << "You are " << ljaar << " years, " << lmaand << " months and " << ldag << " days old.";
}
if(ldag == 1 && lmaand == 1)
{
cout << "You are " << ljaar << " years, " << lmaand << " month and " << ldag << " day old.";
}
if(ldag != 1 && lmaand == 1)
{
cout << "You are " << ljaar << " years, " << lmaand << " month and " << ldag << " days old.";
}
if(ldag == 1 && lmaand != 1)
{
cout << "You are " << ljaar << " years, " << lmaand << " months and " << ldag << " day old.";
}
if(ljaar == 0 && lmaand == 0 && ldag == 0)
{
cout << endl << "You are just born!" << endl;
}
if(ljaar != 0 && lmaand == 0 && ldag == 0)
{
cout << endl << endl << "Happy birthday! You have become " << ljaar << " years old now!" << endl;
}
cout << endl << endl;
system("pause");
return 0;
}