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.

Recommended Answers

All 4 Replies

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)

Please insert a double '=' to make it work. IE turn==1

Thanks for the tip man got it to work!

= 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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.