Code:
#include <iostream>
using namespace std;
int main(){
int birth_year,birth_month,birth_day;
//current date info
// ( in reality this should not be hardcoded )
int year = 2002;
int month = 12;
int day = 26; //reflect current day
cout<<"Enter year you were born (4 digit):";
cin>>birth_year;
if(birth_year > 2002)
{
cout<<"Error: You cannot be born after this year if you are using the program now. See Einstein's clock theory for more details"<<endl;
}
if(birth_year <= 2002)
{
cout<<"Enter month you were born (2 digit):";
cin>>birth_month;
if((birth_month<0) || (birth_month>12))
{
cout<<"Error: In the standardized time format adopted some while ago, you cannot have a negative month or a month having a value more than 12 (unless you're referring to the Simpsons' \"Smarch\""<<endl;
}
if((birth_month>0) && (birth_month <= 12))
{
cout<<"Enter day you were born (2 digit):";
cin>>birth_day;
if(birth_day < 0 || birth_day > 31)
{
cout<<"Error: In the standardized time format adopted some while ago, you cannot have a negative day or a day having an integral value exceeding that of 31"<<endl;
}
else
{
int age = year - birth_year;
if(birth_month < month) age-- ;
else if(birth_month == month && birth_day < day) age-- ;
cout<<"You are " << age << " years old " << endl;
}
}
return 0;
}
}
Just as a little revision to beege31337's code, You might want to guard against negative/exceeding years, negative/exceeding days, and negative/exceeding months. This was hacked together quite quickly so I don't know if it'll compile, but it should work.