Hey guys I'm trying to create a tic-tac-toe program in c++. This is what I have so far. But it just produces garbage.

#include<iostream>
#include<cstdlib>
#include<ctime>
#define frz system("PAUSE")
using namespace std;

//Global Variables
const int length = 3;
const int width = 3;


//Function Prototypes 
void initializeboard(char [length][width], int);
void displayboard(char [length][width], int);
void playermove(char [length][width], int);
void computermove(char [length][width], int);
void determinewinner(char [length][width], int, bool);


int main()
{
	bool gamefinished = false;

	char board[length][width];

	initializeboard(board, width);

	cout << "Welcome to tic-tac-toe\n";


	while(gamefinished = false)
	{
		bool gamecontinue = false;

		for (int a = 0; a < length; a++)
		{
			for(int i = 0; i < width; i++)
			{
				if(board[a][i] = '*')
					gamecontinue = true;
				
			}

		
		}

		if(gamecontinue)
		{
			playermove(board, width);
			determinewinner(board, width, gamefinished);
			computermove(board, width);
			determinewinner(board, width, gamefinished);

		}

		else 
			cout << "The game is over, it's a draw\n";

	
	}

	frz;
	return 0;
	
}

void initializeboard(char board[length][width], int width)
{
	for (int a = 0; a < length; a++)
	{
		for(int i = 0; i < width; i++)
			board[a][i] = '*';
	}

}

void displayboard(char board[length][width], int width)
{

	for (int a = 0; a < length; a++)
	{
		for(int i = 0; i < width; i++)
			cout << board[a][i];
		cout << endl;
	}

}

void playermove(char board[length][width], int width)
{
	int x , y;
	displayboard(board, width);
	cout << "Choose a position on the board in terms of x,y\n";
	cin >> x >> y;

	while(board[x][y] != '*')
	{
		cout << "Sorry, that position is already taken. Choose another position\n";
		cin >> x >> y;
	}

	board[x][y] = 'X';

}

void determinewinner(char board[length][width], int width, bool gamefinished)
{
	if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')
	{
		cout << "Player 1 has won\n";
		gamefinished = true;
		

	}
	if (board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X')
	{
		cout << "Player 1 has won\n";
		gamefinished = true;
		

	}
	if (board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
	{
		cout << "Player 1 has won\n";
		gamefinished = true;
		

	}
	if (board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X')
	{
		cout << "Player 1 has won\n";
		gamefinished = true;
		

	}
	if (board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X')
	{
		cout << "Player 1 has won\n";
		gamefinished = true;
		

	}
	if (board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X')
	{
		cout << "Player 1 has won\n";
		gamefinished = true;
		

	}
	if (board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X')
	{
		cout << "Player 1 has won\n";
		gamefinished = true;
		

	}
	if (board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X')
	{
		cout << "Player 1 has won\n";
		gamefinished = true;
		

	}
	if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')
	{
		cout << "Player 2 has won\n";
		gamefinished = true;
		

	}
	if (board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O')
	{
		cout << "Player 2 has won\n";
		gamefinished = true;
				

	}
	if (board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O')
	{
		cout << "Player 2 has won\n";
		gamefinished = true;
				

	}
	if (board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O')
	{
		cout << "Player 2 has won\n";
		gamefinished = true;
				

	}
	if (board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O')
	{
		cout << "Player 2 has won\n";
		gamefinished = true;
				

	}
	if (board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O')
	{
		cout << "Player 2 has won\n";
		gamefinished = true;
				

	}
	if (board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O')
	{
		cout << "Player 2 has won\n";
		gamefinished = true;
				

	}
	if (board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O')
	{
		cout << "Player 2 has won\n";
		gamefinished = true;
				

	}


}

void computermove(char board[length][width], int width)
{
	unsigned seed = time(0);
	srand(seed);

	int x = 1 + rand() % 2;
	int y = 1 + rand() % 2;

	while(board[x-1][y-1] != '*')
	{
		int x = 1 + rand() % 2;
		int y = 1 + rand() % 2;
    }

	board[x-1][y-1] = 'O';



}

Produces garbage where? What is happening that shouldn't, or vice versa?

Lines 226 and 227 - You should seed ONCE. Move these lines to line 22.

void determinewinner(char board[length][width], int width, bool gamefinished)

Note that gamefinished is NOT passed by reference and hence changing it in the function does absolutely nothing. Either have the function RETURN gamefinished and harness that return that value or pass that variable by reference so it will have an effect on what happens in main.

Line 31 - The big one. Beware = versus ==. You are comparing values. You want to use ==.

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.