I`m trying to program this tic tac toe game, I have the screan printing like I want it to, but I don`t know how to take the user`s input and change the numbers in the array to an "X" or "O" depending on the user`s input, and upgrade the grid, I want to have player1, and player2, "X" for player1 and "O" for player two.

Can anybody help me?

#include <iostream>
#include <iomanip>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

using std::setw;

//global variables
const int row = 3; //size of array
const int column = 3; // size of array

void ticTacToe(const string ticTacToe[][column]); //Function prototypes

// program execution begins
int main()
{
	int player1;
	int player2;
	string ticTac[row][column] = {{"1","2","3"}, {"4","5","6"}, {"7","8","9"}}; //number display for rows and columns 
	cout << "Welcome to the tic tac toe game "<<endl << endl; // welcome message
	ticTacToe( ticTac ); // function call

	return 0;
}

//Function ticTacToe
void ticTacToe(const string ticTacToe[][column])
{
	cout << setw(5)<<"|"<<setw(5)<< "|" <<endl;

	//for loop-continuation condition and increment
	for (int i = 0; i <row; i ++)
	{
		for (int j = 0; j < column; j++)
			//check to output '|' only in between the numbers, not at the end
			if ( j < column - 1)
			{
				cout << setw(3)<< ticTacToe[i][j] << setw(2)<<"|";
			}
			else					
				cout << setw(3)<<ticTacToe[i][j];
							
			cout << endl;

			// check to make sure the horizontal line in only printed in between
			// the 3 rows, not on the bottom fo the picture
			if ( i < row -1)
			{
				cout << "____|____|____"<<endl;
				cout << setw(5) << "|" << setw(5) << "|" << endl;
			}
			else
				cout << setw(5) << "|" << setw(5) << "|" << endl;		
			
	}
}

what's the problem?

what's the problem?

there`s no problem with this code, but I don`t know how to go any further, I`d like to get an Idea on how to get the user`s input and change the number in the grid to an "X" if it`s player1, or "O" if player2.
I`m just a biginner, I`m not sure how to do that, I was thinking on seting a switch statement, but it seems like I can`t do that since I have the number stored in the array.
I`ll be glad if you can help me

Work the problem in steps.

I see the steps as something like:

Prompt the user for where to put their symbol

Accept the user input

Validate that they entered something acceptable and that the location they asked for is available

Set the location to their character

You would then probably redraw the board.

This would be a good point to check for a winner (if that's something you need to do).

If there are still available spaces on the board, you would repeat the above for the 'other' player.

If you see a different set of steps, go ahead and use yours, but a brief outline like this for how you expect the program to work does wonders for keeping yourself aligned.

In any case, pick a step, write it and test it. If it doesn't work, try to figure it out. If you can't figure it out, post that part of your code, tell us what you want it to do, what it does now and the other things you tried.

For projects like these (that we all presume are homework) you get much better help when you demonstrate your work.

I changed my code a little, instead of numbers i have the same string arrays, but they are BLANK, and i have a global variable string BLANK initialized to " "( a blank space ), so my board doesn't have any numbers.

Then I promp the user for coordinates, and store the coordinates in x and y, but when I call the function in main I get an error.

this is the function where I promp the user for input:

void player1()
{
    int x = 0;
    int y = 0;
    string p1 = "X";

    cout << "Player one can enter your coordinates for X." << endl;
        cin >> x >> y;
        if (ticTac[x][y] != BLANK)
        {
            cout << "That spot is taken." << "\nEnter different coordinates for X." << endl;
            player1();
        }
        else
            ticTac[x][y] = p1;
}

in main I call it like this:

player1();

but I get an error:

Error   1   error C2064: term does not evaluate to a function taking 0 arguments    c:\documents and settings\fernandos computer\desktop\tic_tac\tic_tac\tic_tac.cpp    29

can you help me with that

do you still have the variable player1 in main? it will hide the new function.

I want to call functions player1()and player2() until ticTac[][] has at least one BLANK spot.
can anybody help me, this is not working

int main()
{
	string ticTac[row][column] = {{BLANK,BLANK,BLANK}, {BLANK,BLANK,BLANK}, {BLANK,BLANK,BLANK}}; //number display for rows and columns 
	cout << "Welcome to the tic tac toe game "<<endl << endl; // welcome message
	ticTacToe( ticTac ); // function call
	if (ticTac[row][column] = BLANK) 
	{
		player1(ticTac);
		player2(ticTac);
	}
	else
		cout << endl;
	

	return 0;
}

when I compile this program I have the following error message:

Error 1 error C2451: conditional expression of type 'std::basic_string<_Elem,_Traits,_Ax>' is illegal c:\documents and settings\fernandos computer\desktop\tic_tac\tic_tac\tic_tac.cpp 28

You need to put the strings in " ", so "BLANK","BLANK", etc. EDIT: It was in your prior posts, I overlooked it, sorry!

I did that in the beginning of the code:
string BLANK = " ";

I initialized every string BLANK to " "

Apologies, I realized that you had probably defined it somewhere else once I had written that... It's the assignment statement = (on line 6) and you want the == (*I'm not awake yet here)

This is the code I got now, the last function if commented out because it`s not done yet, I want to get what ever is in main working properly before I finish the checkWinner function, the problem now is that it continues to loop even when the grid is full of "X and "O", I want it to jump out of the loop and end once it`s full.

can you help me

#include <iostream>
#include <iomanip>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

using std::setw;

//global variables
const int row = 3; //size of array
const int column = 3; // size of array
string BLANK = " ";
string ticTac[row][column];

void ticTacToe(const string ticTacToe[][column]); //Function prototypes
void player1(string ticTacToe[][column]);
void player2(string ticTac[][column]);
void checkWinner(string ticTac[][column]);

// program execution begins
int main()
{
	string ticTac[row][column] = {{BLANK,BLANK,BLANK}, {BLANK,BLANK,BLANK}, {BLANK,BLANK,BLANK}}; //number display for rows and columns 
	cout << "Welcome to the tic tac toe game "<<endl << endl; // welcome message
	ticTacToe( ticTac ); // function call
	while (ticTac[row][column] != BLANK) 	
	{		
		player1(ticTac);		
		player2(ticTac);	
	}			
		cout << endl; 
	

	return 0;
}

//Function ticTacToe
void ticTacToe(const string ticTacToe[][column])
{
	cout << setw(5)<<"|"<<setw(5)<< "|" <<endl;

	//for loop-continuation condition and increment
	for (int i = 0; i < row; i ++)
	{
		for (int j = 0; j < column; j++)
			//check to output '|' only in between the numbers, not at the end
			if ( j < column - 1)
			{
				cout << setw(3)<< ticTacToe[i][j] << setw(2)<<"|";
			}
			else					
				cout << setw(3)<<ticTacToe[i][j];
							
			cout << endl;

			// check to make sure the horizontal line in only printed in between
			// the 3 rows, not on the bottom fo the picture
			if ( i < row -1)
			{
				cout << "____|____|____"<<endl;
				cout << setw(5) << "|" << setw(5) << "|" << endl;
			}
			else
				cout << setw(5) << "|" << setw(5) << "|" << endl;		
			
	}
}

void player1(string ticTac[][column])
{
	int x = 0;
	int y = 0;
	string p1 = "X";

	cout << "Player one can enter your coordinates for X." << endl;
		cin >> x >> y ;
		x--, y--;
		while (x >= row || y >= column)
		{
			cout << "Invalit entry, try a number from 1 to 3.";
			cin >> x >> y;
			x--, y--;
		}
		if (ticTac[x][y] != BLANK)
		{
			cout << "That spot is taken." << "\nEnter different coordinates for 'X'." << endl;
			player1(ticTac);
		}
		else
			ticTac[x][y] = p1;
		ticTacToe( ticTac );
		
}

void player2(string ticTac[][column])
{
	int x = 0;
	int y = 0;
	string p2 = "O";

	cout << "Player two can enter you coordinates for 'O'. ";
		cin >> x >> y;
	x--,y--;
		while  (x >= row || y >= column)
		{
			cout << "Invalit entry, try a number from 1 to 3.";
			cin >> x >> y;
			x--, y--;
		}
		if (ticTac[x][y] != BLANK)
		{
			cout << "That spot is taken." << "\nEnter different coordinates for 'O'." << endl;
			player2(ticTac);
		}
		else
			ticTac[x][y] = p2;
		ticTacToe( ticTac );
}

/*void checkWinner(const string ticTac[][column])
{
	for (int i = 0; i > row; i++)
	{
		for (int j = 0; j > column; j++)
		{
			//check rows for winner
		   if (ticTac[0][0] == ticTac[1][0] && ticTac[1][0] == ticTac[2][0])
		   {
			   cout << "YOU ARE THE WINNER!";
		   }
		   else if (ticTac[0][1] == ticTac[1][1] && ticTac[1][1] == ticTac[2][1])
		   {
			   cout << "YOU ARE THE WINNER!";
		   }
		   else if (ticTac[0][2] == ticTac[1][2] && ticTac[1][1] == ticTac[2][2])
		   {
				cout << "YOU ARE THE WINNER!";
		   }
		   //check for winner diagnally
		   else if (ticTac[2][0] == ticTac[1][1] && ticTac[1][1] == ticTac[0][2])
		   {
				cout << "YOU ARE THE WINNER!";
		   }
		   else if ([0][0] == ticTac[1][1] && ticTac[1][2] == ticTac[2][2])
		   {
				cout << "YOU ARE THE WINNER!";
		   }
			//check columns for winner
		   else if ([0][0] == ticTac[0][1] && ticTac[0][1] == ticTac[0][2])
		   {
				cout << "YOU ARE THE WINNER!";
		   }
		   else if ([1][0] == ticTac[1][1] && ticTac[1][1] == ticTac[1][2])
		   {
				cout << "YOU ARE THE WINNER!";
		   }
		   else if ([2][0] == ticTac[2][1] && ticTac[2][1] == ticTac[2][2])
		   {
				cout << "YOU ARE THE WINNER!";
		   }
		}
	}
}*/

Your while in main() will never exit since the progress of your inner while loop is not visible to it (is happening inside of the function). Move the while loop condition inside of the function or better yet, have your player1() and player2() functions return a bool if the player has won and use that to control the while loop in main(). Or you could change your checkWin() function to return a bool and do the same thing.

I agree about the while in main, but were you trying to stop when the board was full?

You can't look at a single location to determine if the board is full.

Just another comment or two for future projects (I wouldn't re-design this one now):

Just because the board is displayed as 3 x 3 on the screen, does not require you to maintain it as 3 x 3 in memory.

There are several functions which might be simplified if you had a single array with 9 elements. For example, your winner check for rows if you had a single array it would look like:

if ( (ticTac[0] == ticTac[1] && ticTac[1] == ticTac[2]) ||
     (ticTac[3] == ticTac[4] && ticTac[4] == ticTac[5]) ||
     (ticTac[6] == ticTac[7] && ticTac[7] == ticTac[8]) )
{
   // Horizontal winner
}

It would also allow you to replace most (if not all) of your nested for loops with a single loop.

It would also make it easier to allow the user to enter 1 - 9 to select the spot they want instead of using two coordinates.

When I wrote one of these last, I used the BLANK concept to know where the available spots were, but my board printer function would see the BLANK and output the index number for the box instead of the blank. That put all of the numbers where you can see them until they are replaced with an X or O.

In player1() and player2() you are using recursion if the space they select is already full. It would cause the board to be printed multiple times after they successfully select a spot. It would be better to add the test in with the "is the number in range" test if you can.

It has been my habit to write validating input code as a do-while loop. Inside the loop, you prompt for and accept input then set 'valid' to true. You then execute as many tests as necessary, generating output for validation failures and setting 'valid' to false. The while (! valid) at the end of the loop causes it to go back and ask for input again if the input failed any of the tests.

Also, not to be picky, but just what is the difference between the player1() and player2()? It displays 'X' or 'O' for the location prompt and it sets the actual board location to 'X' or 'O'? Why not pass the 'X' or 'O' in to a single function. (I hate maintaining functions that are almost identical if I don't have to.)

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.