Hi guys
I just started writing in c++ yesterday, although I did write in visual basic before.
After watching some basic c++ youtube video's i decided i wanted to make my first game, tic tac toe. So as a warning i did this without using anyone else's code or any tutorials help so it may be a little scary... but i was wondering why my if statment went onto the else so that it would become the "O" turn. Also if anyone has any hints on how i could write better, it would be appreciated.

#include "stdafx.h"
#include <iostream>

using namespace std;
char Square [9];

bool wingame()
{
	if(((Square[0] = 'X') && (Square[1] = 'X') && (Square[2] = 'X')) || ((Square[3] = 'X') && (Square[4] = 'X') && (Square[5] = 'X'))
	|| ((Square[6] = 'X') && (Square[7] = 'X') && (Square[8] = 'X')) || ((Square[0] = 'X') && (Square[3] = 'X') && (Square[6] = 'X'))
	|| ((Square[1] = 'X') && (Square[4] = 'X') && (Square[7] = 'X')) || ((Square[2] = 'X') && (Square[5] = 'X') && (Square[8] = 'X'))
	|| ((Square[0] = 'X') && (Square[4] = 'X') && (Square[8] = 'X')) || ((Square[2] = 'X') && (Square[4] = 'X') && (Square[6] = 'X')));
	
	{
	return false;
	}
	
	if(((Square[0] = 'O') && (Square[1] = 'O') && (Square[2] = 'O')) || ((Square[3] = 'O') && (Square[4] = 'O') && (Square[5] = 'O'))
	|| ((Square[6] = 'O') && (Square[7] = 'O') && (Square[8] = 'O')) || ((Square[0] = 'O') && (Square[3] = 'O') && (Square[6] = 'O'))
	|| ((Square[1] = 'O') && (Square[4] = 'O') && (Square[7] = 'O')) || ((Square[2] = 'O') && (Square[5] = 'O') && (Square[8] = 'O'))
	|| ((Square[0] = 'O') && (Square[4] = 'O') && (Square[8] = 'O')) || ((Square[2] = 'O') && (Square[4] = 'O') && (Square[6] = 'O')));
	
	{
	return false;
	}
	return true;
}





int main()
{
	
	bool PlayerTurn = true;
	int move;
	
	
	Square [0] = '0';
	Square [1] = '1';
	Square [2] = '2';
	Square [3] = '3';
	Square [4] = '4';
	Square [5] = '5';
	Square [6] = '6';
	Square [7] = '7';
	Square [8] = '8';
	
	cout << Square[0] << Square[1] << Square[2] << endl;
	cout << Square[3] << Square[4] << Square[5] << endl;
	cout << Square[6] << Square[7] << Square[8] << endl;

	while (wingame)
	{
	if(PlayerTurn = true)
	{
		PlayerTurn = false;
		cout << "X turn, Enter a number";
		cin >> move;
			switch(move)
			{
			case (0):
				Square[0] = 'X';
				break;
			case (1):
				Square[1] = 'X';
				break;
			case (2):
				Square[2] = 'X';
				break;
			case (3):
				Square[3] = 'X';
				break;
			case (4):
				Square[4] = 'X';
				break;
			case (5):
				Square[5] = 'X';
				break;
			case (6):
				Square[6] = 'X';
				break;
			case (7):
				Square[7] = 'X';
				break;
			case (8):
				Square[8] = 'X';
				break;
			}
	cout << Square[0] << Square[1] << Square[2] << endl;
	cout << Square[3] << Square[4] << Square[5] << endl;
	cout << Square[6] << Square[7] << Square[8] << endl;
	
	}
	else (PlayerTurn = false);
	{
		PlayerTurn = true;
		cout << "O turn, Enter a number";
		cin >> move;
			switch(move)
			{
			case (0):
				Square[0] = 'O';
				break;
			case (1):
				Square[1] = 'O';
				break;
			case (2):
				Square[2] = 'O';
				break;
			case (3):
				Square[3] = 'O';
				break;
			case (4):
				Square[4] = 'O';
				break;
			case (5):
				Square[5] = 'O';
				break;
			case (6):
				Square[6] = 'O';
				break;
			case (7):
				Square[7] = 'O';
				break;
			case (8):
				Square[8] = 'O';
				break;
			}
	cout << Square[0] << Square[1] << Square[2] << endl;
	cout << Square[3] << Square[4] << Square[5] << endl;
	cout << Square[6] << Square[7] << Square[8] << endl;
		
	}
	}

	system("PAUSE");
}

== is used to test for equality, = is an assignment operator, also else doesn't take a condition, so it would just be:

if(PlayerTurn == true)
    //etc.
else
    //etc.

In fact, you could write it:

if(PlayerTurn)
    //etc.
else
    //etc.

because it's already a boolean variable.

Also, no ; after the else statement.

Edited 5 Years Ago by jonsca: n/a

Okay i got that working, but for some reason the code for winning doesn't work.
This is my new code all together.

#include "stdafx.h"
#include <iostream>

using namespace std;
char Square [9];

bool wingame()
{
	if(((Square[0] == 'X') && (Square[1] == 'X') && (Square[2] == 'X')) || ((Square[3] == 'X') && (Square[4] == 'X') && (Square[5] == 'X'))
	|| ((Square[6] == 'X') && (Square[7] == 'X') && (Square[8] == 'X')) || ((Square[0] == 'X') && (Square[3] == 'X') && (Square[6] == 'X'))
	|| ((Square[1] == 'X') && (Square[4] == 'X') && (Square[7] == 'X')) || ((Square[2] == 'X') && (Square[5] == 'X') && (Square[8] == 'X'))
	|| ((Square[0] == 'X') && (Square[4] == 'X') && (Square[8] == 'X')) || ((Square[2] == 'X') && (Square[4] == 'X') && (Square[6] == 'X')));
	
	{
	return false;
	}
	
	if(((Square[0] == 'O') && (Square[1] == 'O') && (Square[2] == 'O')) || ((Square[3] == 'O') && (Square[4] == 'O') && (Square[5] == 'O'))
	|| ((Square[6] == 'O') && (Square[7] == 'O') && (Square[8] == 'O')) || ((Square[0] == 'O') && (Square[3] == 'O') && (Square[6] == 'O'))
	|| ((Square[1] == 'O') && (Square[4] == 'O') && (Square[7] == 'O')) || ((Square[2] == 'O') && (Square[5] == 'O') && (Square[8] == 'O'))
	|| ((Square[0] == 'O') && (Square[4] == 'O') && (Square[8] == 'O')) || ((Square[2] == 'O') && (Square[4] == 'O') && (Square[6] == 'O')));
	
	{
	return false;
	}
	return true;
	
}





int main()
{
	
	bool PlayerTurn = true;
	int move;
	
	
	Square [0] = '0';
	Square [1] = '1';
	Square [2] = '2';
	Square [3] = '3';
	Square [4] = '4';
	Square [5] = '5';
	Square [6] = '6';
	Square [7] = '7';
	Square [8] = '8';
	
	cout << Square[0] << Square[1] << Square[2] << endl;
	cout << Square[3] << Square[4] << Square[5] << endl;
	cout << Square[6] << Square[7] << Square[8] << endl;

	while (wingame)
	{
	if(PlayerTurn == true)
	{
		PlayerTurn = false;
		cout << "X turn, Enter a number";
		cin >> move;
			switch(move)
			{
			case (0):
				Square[0] = 'X';
				break;
			case (1):
				Square[1] = 'X';
				break;
			case (2):
				Square[2] = 'X';
				break;
			case (3):
				Square[3] = 'X';
				break;
			case (4):
				Square[4] = 'X';
				break;
			case (5):
				Square[5] = 'X';
				break;
			case (6):
				Square[6] = 'X';
				break;
			case (7):
				Square[7] = 'X';
				break;
			case (8):
				Square[8] = 'X';
				break;
			}
	cout << Square[0] << Square[1] << Square[2] << endl;
	cout << Square[3] << Square[4] << Square[5] << endl;
	cout << Square[6] << Square[7] << Square[8] << endl;
	
	}
	else
	{
		PlayerTurn = true;
		cout << "O turn, Enter a number";
		cin >> move;
			switch(move)
			{
			case (0):
				Square[0] = 'O';
				break;
			case (1):
				Square[1] = 'O';
				break;
			case (2):
				Square[2] = 'O';
				break;
			case (3):
				Square[3] = 'O';
				break;
			case (4):
				Square[4] = 'O';
				break;
			case (5):
				Square[5] = 'O';
				break;
			case (6):
				Square[6] = 'O';
				break;
			case (7):
				Square[7] = 'O';
				break;
			case (8):
				Square[8] = 'O';
				break;
			}
	cout << Square[0] << Square[1] << Square[2] << endl;
	cout << Square[3] << Square[4] << Square[5] << endl;
	cout << Square[6] << Square[7] << Square[8] << endl;
		
	}
	}

	if (PlayerTurn == true)
	{
		cout << "The Winner is X";
	}
	else
	{
		cout << "The Winner is O";
	}

	system("PAUSE");
}

The semicolons on lines 13 and 22 don't belong there. If you put them there, the if statement will execute the statement with which you intended it to be associated regardless of the condition.

if(lotterynumbers == "123456");   // <--- the ; is associated with the if
    NotifyUserOfWin();    //just a plain ol' statement, lotterynumbers could be "7"
                          //which makes the if statement false, but it gets executed anyway
This question has already been answered. Start a new discussion instead.