Variables not working in conditional statements?

int main()
{
int example = 0;
int condition;

cout << "Type your choice.\n";
cin >> condition;
cout << endl;

if (condition = 1) {
int example = example + 1;
}
else if (condition = 2) {
int example = example + 2;
}
}

When I got to compile and run, the IF statement reports no problems, but the ELSE IF statement reports an unitialized variable.
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
#include <iostream>

int main()
{
    int example = 0; // *** warning: unused variable 'example' (clang++)
                     // *** warning: 'example': local variable is initialized but not referenced (microsoft)
    
    int condition;
    std::cout << "Type your choice.\n";
    std::cin >> condition;

    if (condition = 1) // *** warning: using the result of an assignment as a condition without parentheses (clang++)
                       // *** warning: assignment within conditional expression (microsoft)
                       // note: use '==' to turn this assignment into an equality comparison (clang++)
                       // *** warning : Incorrect operator:  assignment of constant in Boolean context. Consider using '==' instead. (microsoft)
                       // note: place parentheses around the assignment to silence this warning (clang++)
    {
        // note that the name 'example' on line 20 hides 'example' declared on line 5 
        // *** warning: declaration of 'example' hides previous local declaration (microsoft)
        int example = example + 1; // *** warning: variable 'example' is uninitialized when used within its own initialization (clang++)
                                   // *** warning: uninitialized local variable 'example' used (microsoft)
    }
    
    else if (condition = 2) // *** warning: using the result of an assignment as a condition without parentheses (clang++)
                            // *** warning: assignment within conditional expression (microsoft)
                            // note: use '==' to turn this assignment into an equality comparison (clang++)
                            // *** warning : Incorrect operator:  assignment of constant in Boolean context. Consider using '==' instead. (microsoft)
                            // note: place parentheses around the assignment to silence this warning (clang++)
    {
        // note that the name 'example' on line 32 hides 'example' declared on line 5 
        // *** warning: declaration of 'example' hides previous local declaration (microsoft)
        int example = example + 2; // *** warning: variable 'example' is uninitialized when used within its own initialization (clang++)
                                   // *** warning: uninitialized local variable 'example' used (microsoft)
    }
}

http://coliru.stacked-crooked.com/a/be60af167edd9173
http://rextester.com/ZSPRT58106
Here is your code fixed. I'll explain after this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
    int example = 0;
    int condition;

    cout << "Type your choice.\n";
    cin >> condition;
    cout << endl;

    if (condition == 1) {
        example += 1;
    }
    else if (condition == 2) {
        example += 2;
    }
}


The assignment operator = and == (checks if values on either sides are equal then returns true or false) are different.

The assignment operator assigns a value to which you choose to.

The equality operator compares the two values for equality.

And instead of int example = example + 1; declares and initializes the variable exampe AGAIN. Do example += 1; instead. This re-assigns the variable example to itself + 1.
example = example + 1; does the same thing as example += 1;.
Last edited on
@boost lexical cast
Thank you so much! I can't believe I completely missed the fact I was trying to reinitialize the variable, and that all I really had to do was get rid of the "int" before each conditional statement.

Also, thank you for the tid-bit on writing it as += instead. Does this mean -= would subtract one? Would *= and /= do anything then?

@gentleguy
I would say that is mostly preference, but it does seem like keeping everything on one line generally looks nicer.
Last edited on
-= will subtract whatever it is that is on the right hand side, so x -= 5; would subtract 5. It is another way of writing x = x - 5; Likewise, /= will divide by whatever is on the right hand side, and *= will multiply by whatever is on the right hand side.
All provided the operator knows how to do that for the types given.
Topic archived. No new replies allowed.