Code:
/*
**************************************************
***TicTacToe.cpp Program ***
***This is Tic-Tac-Toe. Get 3 in a row in win! ***
***Author: CPTR Class 212 ***
***Steven Davis ***
**************************************************
*/
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
void displayBoard();
char makeUpperCase(char p1);
int main ()
{
string
p1Name,
p2Name;
char
p1,
p2;
int error = 0;
cout << "***TIC-TAC-TOE***\n";
displayBoard();
system("pause");
system("cls");
cout << "Player One, Enter your name: ";
cin >> p1Name;
cout << "\nPlayer Two, Enter your name: ";
cin >> p2Name;
do
{
cout << p1Name << ", would you like to be X's or O's?\n";
/*
Here is my problem. If I hit x or X it works but if i hit anything else
it lets me know there was an error and starts the loop over.
When ever the loop is restarted and it asks me to enter in X or O
I cant. If i hit x or X it just skips it like i didnt enter anything. I
tried to do a cin.clear and cin.ignore but they dont seem to fix
them problem. TIA :)
*/
cin >> p1;
p1 = makeUpperCase(p1);
if (p1 == 'X')
{
p2 = 'O';
}
else if (p1 == 'O')
{
p2 = 'X';
}
else
{
error = 1;
cin.clear();
cin.ignore(1024, '\n');
cout << "\nERROR: Please choose X or O!\n";
}
} while (error == 1); //End While loop for X's and O's
cout << p1Name << " is " << p1 << endl
<< p2Name << " is " << p2 << endl;
return 0;
} //End Main
void displayBoard()
{
char rowOne[10][13] = {
{' ',' ',' ','A',' ',' ',' ','B',' ',' ',' ','C',' '},
{' ',' ',' ',' ',' ','|',' ',' ',' ','|',' ',' ',' '},
{'1',' ',' ',' ',' ','|',' ',' ',' ','|',' ',' ',' '},
{' ',' ','_','_','_','|','_','_','_','|','_','_','_'},
{' ',' ',' ',' ',' ','|',' ',' ',' ','|',' ',' ',' '},
{'2',' ',' ',' ',' ','|',' ',' ',' ','|',' ',' ',' '},
{' ',' ','_','_','_','|','_','_','_','|','_','_','_'},
{' ',' ',' ',' ',' ','|',' ',' ',' ','|',' ',' ',' '},
{'3',' ',' ',' ',' ','|',' ',' ',' ','|',' ',' ',' '},
{' ',' ',' ',' ',' ','|',' ',' ',' ','|',' ',' ',' '},
};
for (int i=0; i < 10; i++)
{
for (int j=0; j < 13; j++)
{
cout << rowOne[i][j];
}//End for loop
cout << endl;
}//End for loop
}// End Display Board
char makeUpperCase(char p1)
{
char uppercase;
switch (p1)
{
case 'x':
uppercase = 'X';
break;
case 'o':
uppercase = 'O';
break;
case 'a':
uppercase = 'A';
break;
case 'b':
uppercase = 'B';
break;
case 'c':
uppercase = 'C';
break;
default:
return p1;
};
return uppercase;
}// End Make Upper Case