I am wanting to provide an option for players to input their names so that they are displayed at the top of the screen and are also prompted with their names when it is their turn to go.
Although with everything that I have tried I seem to always run into the problem that I cannot provide this option without messing up almost all of my code as I have given a global default player with which i have used throughout my code/functions.
I am wandering if there is a way for me to provide them with this option without messing up my code too much.
shouldn't be to hard. try something like this, just change "charName" in my example to whatever you used in your code
1 2 3 4 5 6 7 8 9 10 11
string charName = "Player"; //This give a default name of Player if you want a default name
int main ()
cout << "Player 1's name :\n";
cin >> charName
//than you can do things like display score and player name with
cout << charName << "'s score is 1,000";
this will ask the player for there name, then input there response to charName.
then it will print to the screen
"charName's score is 1,000" (where charName is what they input)
Your problem is in your display function. your code (as follows)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void Display()
{
//so that there is only one grid being displayed (clears previous display once an input is executed)
system("cls");
cout << "Welcome to Tic-Tac-Toe!" << endl;
cout << "X goes first." << endl;
//r = row c = column
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
cout << grid[r][c] << " ";
}
cout << endl;
}
}
should be
[code]
void Display()
{
//so that there is only one grid being displayed (clears previous display once an input is executed)
system("cls");
cout << "Welcome to Tic-Tac-Toe!" << endl;
cout << X << " goes first." << endl;
//r = row c = column
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
cout << grid[r][c] << " ";
}
cout << endl;
}
}
[\code]
Your problem was you had X as part of the literal string.
Hmm I've managed to work around this, thank you very much.
I also have another issue if your willing to help.
I am wanting to have a menu where is asks the player if they would like to play or quit the game.
I thought an if statement would suffice for this but are unsure on what to put for the coding e.g.
int p;
int Play;
int Quit;
cout << "Hello, and welcome to the Menu, " << endl;
cout << "If you would like to play, type 'Play'." << endl;
cout << "Or if you would like to quit, type 'Quit'." << endl;
cin >> p;
if (p == Play)
{
//Start the game
}
else if (p == Quit)
{
//Close window
}
Firstly, you have said Play is an int, but you want the play to type in a String, that wont work.
Same with Quit.
it might be easier using a char so for example
char p;
char play;
char quit;
cout << "Hello, and welcome to the Menu, " << endl;
cout << "If you would like to play, type 'p'." << endl;
cout << "Or if you would like to quit, type 'q'." << endl;
cin >> p;
if (p == 'p')
{
//Start the game
}
else if (p == 'q')
{
//Close window
}
still a beginner so not completely sure with this but hopefully that will help.