0

Cant seem to find the solution to this although its probably starting me straight in the face.

Iv been at this for a few days now,can anyone help me spot the bug?

// test the bool

#include <iostream>
using namespace std;

main(){

    int turn;
    int var=2;

    while(var!=1){
        cout << "1 for x, 2 for o" << endl;
        cin >> turn;


        if(turn=1){
            cout << "X" << endl;
            turn=2; // o's turn
        }

        else if (turn=2){
            cout << "O" << endl;
            turn=1; // x's turn
        }
    }

}

the program will only print out the x,never the o,even if i press 2.Iv tried switching the turn variable to an int too.

4
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by snehil_khanor
1

This is a common mistake whilst performing tests using boolean logic; there is a distinct difference between the '=' assignment operator and the '==' equality test. (the '=' assignment operator should never be used to perform boolean tests)

0

= is assignment operator which assigns a value to a variable ...
for e.g.
x=y; assigns y into x..
and == operator is used for equality
for e.g.
if(x==y)
{
some code;
}


njjoy!!
-Snehil :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.