Hi, this is like only like my third c++ program, so I apologize if it is riddled with errors. The problem I have is that the if statement I have coded doesn't seem to be evaluated even though if you enter a larger BirthMonth than CurretnMonth, any help? anything else that is wrong with the code?
#include <iostream>
usingnamespace std;
int CurrentYear;
int CurrentMonth;
int BirthYear;
int BirthMonth;
int main( )
{
cout<<"Enter the current year: "<< "\n" ;
cin>>CurrentYear;
cout<<"Enter the current month(for example if it was december you would input 12 because it is the 12th month of the year): "<< "\n" ;
cin>>CurrentMonth;
cout<<"Enter your birth year: "<< "\n" ;
cin>>BirthYear;
cout<<"Enter your birth month(for example if it was december you would input 12 because it is the 12th month of the year): "<< "\n" ;
cin>>BirthMonth;
if (BirthMonth<CurrentMonth)
{ cout<<"You are "<<CurrentYear-BirthYear-1<<" years and "<<12-(CurrentMonth-BirthMonth)<<" old";
}
else
{
cout<<"You are "<<CurrentYear-BirthYear<<" years "<< CurrentMonth-BirthMonth<<" months old"<< "\n" ;
return 0;
}
}
That is because you have the evaluation sign the wrong way round on line 28, you are using less than (<) rather than greater than (>). Also, try to avoid using global variables, and put the return 0 outside the if/else block.
EDIT:
Also, you don't need to seperate the '\n' from the strings you are outputting, these are the same: