This is not a complete program, I have comments to omit the incomplete portions. After "Player One" enters a row (see lines 54-5), the program ends. It outputs the "Enter a column" and the board and ends. I don't see why. Any help would be greatly appreciated!

(Output after runtime error is included after code)

void showRules();
char playerOne(char[][3], int, int);
char playerTwo(char[][3], int, int);
int getWinner(char[][3], int, int);
void showBoard(char[][3], int, int);

int main() {

    const int row = 3;
    const int col = 3;
    int winner;
    char game[row][col] = {{'*', '*', '*'},
                           {'*', '*', '*'},
                           {'*', '*', '*'}};
    showRules();
    //do {
        showBoard(game, row, col);
        playerOne(game, row, col);
        //winner = getWinner(game, row, col);
        showBoard(game, row, col);
        playerTwo(game, row, col);
        //winner = getWinner(game, row, col);
   // } while (winner != 1 && winner != 2);


    cout << "\n\n\n\n";

    return 0;
}
/************************************************************/
void showRules() {
    char ch;
    system("clear");
    cout << "\n\n\t\t________________________________"
         <<    "\n\t\t        TIC - TAC - TOE"
         << "\n\n\t\tRules:  Two Player Game"
         <<   "\n\t\t          - Player One (X)"
         <<   "\n\t\t          - Player Two (O)"
         << "\n\n\t\tObject: Place game pieces (X,O)"
         <<   "\n\t\t        in a line of three."
         <<   "\n\t\t          - In a row"
         <<   "\n\t\t          - A column"
         <<   "\n\t\t          - Or diagonally"
         <<   "\n\t\t________________________________"
         <<   "\n\t\t      Press [ENTER] to Play"
         <<   "\n\n";
    cin.get(ch);
}
/************************************************************/
char playerOne(char game[][3], int row, int col){

    cout << "\n\n\t\tPlayer One"
         <<   "\n\t\t------------------------------"
         <<   "\n\t\tEnter a row (A,B, or C): ";
    cin  >> row;
    cin.ignore();
    row = toupper(row);

    if (row == 65) row = 0;
    if (row == 66) row = 1;
    if (row == 67) row = 2;

    cout << "\n\n\t\tEnter a column (1, 2, or 3): ";
    cin  >> col;

    char play = 'X';

    game[row][col - 1] = play;

    return play;
}
/************************************************************/
char playerTwo(char game[][3], int row, int col){

    cout << "\n\n\t\tPlayer Two"
         <<   "\n\t\t------------------------------"
         <<   "\n\t\tEnter a row (A,B, or C): ";
    cin  >> row;
    row = toupper(row);
    cin.ignore();

    if (row == 65) row = 0;
    if (row == 66) row = 1;
    if (row == 67) row = 2;

    cout << "\n\n\t\tEnter a column (1, 2, or 3): ";
    cin  >> col;

    char play = 'O';

    game[row][col - 1] = play;

    return play;
}
/************************************************************/
int getWinner(char game[][3], int row, int col) {


}
/************************************************************/
void showBoard(char game[][3], int row, int col) {

    system("clear");
    cout << "\n\n";
    cout << "\t\t" << setw(7) << "1" << setw(6) << "2" << setw(6) << "3" << "\n\n"
         << "\t\t" << setw(10) << "|" << setw(6) << "|" << "\n"
         << "\t\t" << " A  " << setw(3) << game[0][0] << setw(3) << "|" << setw(3)
         << game[0][1] << setw(3) << "|" << setw(3) << game[0][2] << "\n"
         << "\t\t" << setw(10) << "|" << setw(6) << "|" << "\n"
         << "\t\t" << "    -----------------" << "\n"
         << "\t\t" << setw(10) << "|" << setw(6) << "|" << "\n"
         << "\t\t" << " B  " << setw(3) << game[1][0] << setw(3) << "|" << setw(3)
         << game[1][1] << setw(3) << "|" << setw(3) << game[1][2] << "\n"
         << "\t\t" << setw(10) << "|" << setw(6) << "|" << "\n"
         << "\t\t" << "    -----------------" << "\n"
         << "\t\t" << setw(10) << "|" << setw(6) << "|" << "\n"
         << "\t\t" << " C  " << setw(3) << game[2][0] << setw(3) << "|" << setw(3)
         << game[2][1] << setw(3) << "|" << setw(3) << game[2][2] << "\n"
         << "\t\t" << setw(10) << "|" << setw(6) << "|" << "\n\n\n\n";
}

Output after code line 55 is executed

Enter a column (1, 2, or 3):

                      1     2     3

                         |     |
                 A    *  |  *  |  *
                         |     |
                    -----------------
                         |     |
                 B    *  |  *  |  *
                         |     |
                    -----------------
                         |     |
                 C    *  |  *  |  *
                         |     |





                Player Two
                ------------------------------
                Enter a row (A,B, or C):

                Enter a column (1, 2, or 3):




emiller7@endofuniversecafe:$

Recommended Answers

All 4 Replies

Get rid of all of your system ("clear"); commands (you can put them back in again later after it all works, though lots of people recommend not using the system command at all and there are good threads explaining why). You can't debug well with them in there because you need to know whether you are pausing for input. If you are asking a question and the program is not pausing for input, you may well have an input stream that is not empty. Read this thread by Narue on how to flush the input stream. It's pinned to the top of the C++ forum.

http://www.daniweb.com/forums/thread90228.html

Get rid of the code that clears the screen so you can see what is going on, then see if that is in fact a problem and if so, correct it and see if it makes the other problems go away.

Thanks!

Your basic problem is that you are assuming row is a char when it is actually an int. Additionally, you are using the parameters row and col of the player functions like local variables. Remove them from the function declaration and declare them as locals, making row a char.

Don't compare letters to there ascii codes. Say if (row == 'A') not if (row == 65) . It's both more readable and more portable.

Also, note that your Player1 and Player2 routines are almost identical. Combine them into one and pass in the player number (or possibly the player letter X or O).

Your basic problem is that you are assuming row is a char when it is actually an int. Additionally, you are using the parameters row and col of the player functions like local variables. Remove them from the function declaration and declare them as locals, making row a char.

Don't compare letters to there ascii codes. Say if (row == 'A') not if (row == 65) . It's both more readable and more portable.

Also, note that your Player1 and Player2 routines are almost identical. Combine them into one and pass in the player number (or possibly the player letter X or O).

Thank you for your input! I took your advice on merging player1 and player2 into one function, and also I corrected the ascii usage. Sometimes when I can't get programs to compile I try weird things and forget to change them back! I didn't change the function declaration because the program works now and I really need to move on to the rest of the assignments. If I need help, I know who to ask! Thanks so much for your help nucleon!

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.