Hello everybody!
I made a program which is able to calculate your age. You can find the source code below this text. There is already posted an article like this on http://www.daniweb.com/software-development/cpp/code/331349/c-code-for-simple-age-calculator
But, my program is:
- Without mistakes, it doesn't give your computer wrong instructions.
- More accurate, because my program calculates also the days instead of only the years and months.
I wrote this program in Dutch, and later I translated it into the English language. If you are using the program, everything is in English. But in the source code you can still find Dutch words like "jaar", "maand", "dag" etc. Just ignore them. If you are using the program, you won't see those words, so don't worry.
Here is the source code:
#include <iostream>
using namespace std;
int main()
{
system("CLS");
int bjaar, bmaand, bdag, hjaar, hmaand, hdag, ljaar, lmaand, ldag, tjaar, tmaand, tdag;
char vraag1;
cout << "\n\n\t\t Mark Wierings's Age Calculator \n\n";
cout << "In which year where you born? (Example: 1995) ";
cin >> bjaar;
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(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;
}
cout << endl << "On which day of the month were you born? ";
cin >> bdag;
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;
}
}
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;
}
}
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;
}
}
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;
}
}
}
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;
}
}
}
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(hjaar < bjaar)
{
cout << endl << "Your birthyear can't be bigger than the current year." << endl;
cout << "Enter a valid year: ";
cin >> hjaar;
}
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(hmaand <= 0 || hmaand > 12)
{
system("CLS");
cout << endl << bmaand << " is an invalid value. ";
cout << "1 to 12 are valid values." << endl;
cout << "Enter a valid value: ";
cin >> hmaand;
}
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;
}
cout << endl << "Which day of the month is it now? ";
cin >> hdag;
if(hmaand == 1 || hmaand == 3 || hmaand == 5 || hmaand == 7 || hmaand == 8 || hmaand == 10 || hmaand == 12)
{
while(hdag <= 0 || hdag > 31)
{
system("CLS");
cout << endl << bdag << " is an invalid value for a day of the month." << endl;
cout << "1 to 31 are valid values. Enter a valid value: ";
cin >> bdag;
}
}
if(hmaand == 4 || hmaand == 6 || hmaand == 9 || hmaand == 11)
{
while(hdag <= 0 || hdag > 30)
{
system("CLS");
cout << endl << bdag << " is an invalid value for a day of the month." << endl;
cout << "1 to 30 are valid values. Enter a valid value: ";
cin >> bdag;
}
}
if(hmaand == 2)
{
if(hjaar%4 == 0)
{
if(hjaar%100 == 0 && hjaar%400 != 0)
{
while(hdag <= 0 || hdag > 28)
{
system("CLS");
cout << endl << bdag << " is an invalid value for a day of the month." << endl;
cout << "1 to 28 are valid values. Enter a valid value: ";
cin >> bdag;
}
}
else
{
while(hdag <= 0 || hdag > 29)
{
system("CLS");
cout << endl << bdag << " is an invalid value for a day of the month." << endl;
cout << "1 to 29 are valid values. Enter a valid value: ";
cin >> bdag;
}
}
}
else
{
while(hdag <= 0 || hdag > 28)
{
system("CLS");
cout << endl << bdag << " is an invalid value for a day of the month." << endl;
cout << "1 to 28 are valid values. Enter a valid value: ";
cin >> bdag;
}
}
}
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;
}
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;
}
To use this program, follow these instructions.
- Copy the source code.
- Paste the source code into a compiler. (For example: Dev-C++, Code::Blocks, Visual Studio or whatever you like to use)
- Save it, give it a name, compile it, use it.
- Have fun.
I am a beginner with C++, so if you think there is an easier way to write this program, let me know. :-D