codedude
codedude

Reputation: 6519

C++ variable value not changing

I'm using the following code (it's been super simplified to get to the root of my problem).

#include <iostream>
namespace std;

int user;
int submit(int);

int main() {

    user = 1;
    submit(user);

    user = 2;
    submit(user);

    return(0);
}

int submit(int user) {

    if (user = 1) {
        printf("1");
    } else if (user = 2) {
        printf("2");
    }
    return(0);

}

I thought that this would print out "12" but instead I'm getting "11". Isn't the variable "user" getting redefined before the function is called for the second time?

What's going wrong here?

Upvotes: 0

Views: 2562

Answers (3)

M Asad Ali
M Asad Ali

Reputation: 106

As answered by the experts, You are using = instead of using == in your function body that's why you are getting wrong output.

Here, I would like to clear your concept why it happens: I hope you know the difference between assignment operator and equality operator. If not, I'm going to describe it briefly.

Assignment operator (=):

The assignment operator assigns a value to a variable. e.g.

user = 1;

This statement assigns the integer value 1 to the variable user.

This statement will always be executed which indicates that it is logically TRUE statement (assuming variable user is declared already).

As there is no comparison or something like that, so if we use Assignment operator(=) as a condition, it will always return TRUE or 1

Equality operator(==):

The equality operator is used to compare two values to know if they are equal or not.

user == 1;

This statement will compare the value of variable user with 1 and it will return TRUE if the value of user is 1 otherwise it will return FALSE.

RESULT: Assignment operator will always return TRUE but comparison operator may return TRUE or FALSE.

Now coming back to your code:

int submit(int user) {
//as you're using assignmnt operator this if condition will always true regardless of input
    if (user = 1) {    
       printf("1");
//because if condition is true, it will never go into else if condition
    } else if (user = 2) {
       printf("2");
    }
    return(0);
}

So, actually, whenever you call this function, it will print 1 each time regardless of the value of user passed to this function. Since, you have called this function 2 times. Therefore, it will print 11.

Upvotes: 1

asheeshr
asheeshr

Reputation: 4114

You are using = not == in your function body.

if (user = 1) { //This assigns user the value of 1 and then prints 1
         printf("1");

The correct test condition should be :

if (user == 1) { //This checks the value of user and then prints if the condition is true
         printf("1");

While compiling, if using gcc, adding the option -Wall is helpful in such cases as it gives you a warning about assignments in test conditions.

Upvotes: 6

Keith Randall
Keith Randall

Reputation: 23265

Use ==, not = to check the values of user. You're overwriting the values (with =) instead of comparing them (with ==).

Upvotes: 8

Related Questions