When I run this, the numbers 1-9 don’t change to either X or O when you pick them. They just stay as numbers. Is it a problem with trying to change the char values in a void function? Not sure how to accomplish this without using a void function.
#include <iostream>
#include <string>
using namespace std;
char vert = 0xB3;
char hori = 0xC4;
char cros = 0xC5;
char one = '1';
char two = '2';
char thr = '3';
char fou = '4';
char fiv = '5';
char six = '6';
char sev = '7';
char eig = '8';
char nin = '9';
char a;
void board()
{
system("cls");
cout << one << vert << two << vert << thr << endl
<< hori << cros << hori << cros << hori << endl
<< fou << vert << fiv << vert << six << endl
<< hori << cros << hori << cros << hori << endl
<< sev << vert << eig << vert << nin << endl;
}
void player1pick()
{
cout << "First Player's turn. Please pick a box: ";
cin >> a;
if(a == 1)
{
one = 'X';
}
if(a == 2)
{
two = 'X';
}
if(a == 3)
{
thr = 'X';
}
if(a == 4)
{
fou = 'X';
}
if(a == 5)
{
fiv = 'X';
}
if(a == 6)
{
six = 'X';
}
if(a == 7)
{
sev = 'X';
}
if(a == 8)
{
eig = 'X';
}
if(a == 9)
{
nin = 'X';
}
board();
}
void player2pick()
{
cout << "Second Player's Turn. Please pick a box: ";
cin >> a;
if(a == 1)
{
one = 'O';
}
if(a == 2)
{
two = 'O';
}
if(a == 3)
{
thr = 'O';
}
if(a == 4)
{
fou = 'O';
}
if(a == 5)
{
fiv = 'O';
}
if(a == 6)
{
six = 'O';
}
if(a == 7)
{
sev = 'O';
}
if(a == 8)
{
eig = 'O';
}
if(a == 9)
{
nin = 'O';
}
board();
}
int main()
{
board();
player1pick();
player2pick();
player1pick();
player2pick();
player1pick();
player2pick();
player1pick();
player2pick();
player1pick();
return 0;
}
That’s because a is a char and you compare it with 1 which is a number. For example, when player enters fo 1 as an input, ‘1’ gets stored in a (which is 49 in ascii). And when you do comparison, e.g.
if(a == 1)it basically compares ascii value of ‘1’ with 1…. so49 == 1which is of course false. Sou, you either have to change type of a to int, or compare char with char –a == '1'.