if statement trouble

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 #include <iostream>

using namespace 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:
1
2
cout << "Hello!" << "\n";
cout << "Hello!\n";
Last edited on
Could you give an example of input and output that is not as you expect?
Thanks for the tip and yes I do now see that the sign was the wrong way...guess I won't code at 12:00am anymore, anyway thanks.
Topic archived. No new replies allowed.