NEED TO ADD 1 Condition

closed account (91AfSL3A)
This program functions except I am having trouble returning the value for February which needs to display "28 or 29 days" it current skips to display 30 days.

Note Booleen variable is a condition of this assignment

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>
#include <string>

using namespace std;

int main ()
{
    int month;
    int month2;
    bool monthsWith31 = (month == 1 || month == 3  || month == 5
    || month == 7 || month == 8  || month == 10  || month == 12);

    string repeat = "y";
    while (repeat == "y" || repeat == "Y"){

    cout << "Enter the month (1-12): ";
    cin >> month;

    cout << boolalpha; // output true or false for bool

    if (month == month2)
    {
    cout << "28 or 29 days";
    }
     else if (monthsWith31) {
    cout << "31 days";
    } else {
		cout << "30 days";
        cout << endl;
    }

    cout << endl;
    cout << "Run again? (y/n) ";
    cin >> repeat;
    cout << endl;
    }

    return 0;
}
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...

 
    if (month == month2)


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?
Last edited on
As @Disch pointed out you are using uninitialized variable month in expression

1
2
bool monthsWith31 = (month == 1 || month == 3  || month == 5
    || month == 7 || month == 8  || month == 10  || month == 12);


If you indeed need to use such an expression you can rewrite it as a lambda-expression. For example

1
2
3
4
5
auto monthsWith31 = []( int month )
{
   return ( month == 1 || month == 3  || month == 5 || month == 7 ||
                month == 8  || month == 10  || month == 12 );
};


This lambda-expression you could use in your code passing a value of month to the expression

For example

std::cout << std::boolalpha << monthsWith31( 7 ) << std::endl;
closed account (91AfSL3A)
Thanks this was useful:
For example

std::cout << std::boolalpha << monthsWith31( 7 ) << std::endl;

and I was trying to use the literal value 2.
Thanks all errors cleared.
Topic archived. No new replies allowed.