Programs execute in a top-down sequential order. Things that you put first will execute first. Doing things further down does not change what had already happened above.
Take a look at line 8-10:
1 2 3 4
|
int month;
int month2;
bool monthsWith31 = (month == 1 || month == 3 || month == 5
|| month == 7 || month == 8 || month == 10 || month == 12);
|
'month' is uninitialized. This means it has a meaningless/garbage value stored in it by default.
'month2' is also uninitialzed, which means it is also garbage.
'monthsWith31' is comparing 'month' to several different months... however the value currently in 'month' is garbage, so these comparisons are all worthless. Therefore 'monthsWith31' ends up also being garbage.
'month' does not have any meaningful value until line 17, where you finally initialize it with user input:
1 2
|
cout << "Enter the month (1-12): ";
cin >> month; // <- this initializes 'month'
|
But then on line 21...
month is initialized (good)... but 'month2' never was (bad)... so you are comparing your 'month' to a random garbage value (meaningless, bad).
And again.. since monthsWith31 is also filled with garbage at this point... the 'if' on line 25 also is meaningless.
You need to initialize/assign 'monthsWith31'
after you give 'month' a value. Then if 'month' changes... you'll need to
reassign 'monthsWith31'.
And I don't know what you were trying to do with 'month2' since you never seem to be setting that variable. Maybe you just meant to use the literal value of
2
to represent February?