Tic tac toe confusion

Pages: 1... 56789... 13
> How will we prompt them so they enter until they do it correctly?
Had you tried the while loop I suggested, you would have been able to understand it.

True?
The only thing I don't understand is the continue part.

We don't use that.
The continue keyword forces the next iteration of the loop to take place, skipping any code in between.

In your case, without the continue keyword, there is no convenient way to make your program jump back to the next loop forcibly (or to prompt the user again), the code continues to execute until it encounters break; and breaks the loop, regardless of the fact that there may be or may not be an error message generated.

Why don't you just do some research and figure out how continue works by try removing it?
> We don't use that.
Many people use it. As you write more complex programs, there are more things you have to learn.
True, but I'm afraid if I use it, I'll lose points since we haven't used it.

He generally wants us to work around what we know, but that sounds like a great idea!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
bool inputSuccess;

// Prompt the player X
do
{
     inputSuccess = true;
     // Your code
     // If anything fails, display an error message and set "inputSuccess" to false
     if(ticTac[positionRow][positionCol] == "*")
     ticTac[positionRow][positionCol] = "X";
     else 
     {
          cout << "That spot has been already chosen!\n\n";
          inputSuccess = false;
     }
     displayBoard(ticTac);
} while (inputSuccess == false); // The loop will continue as long as something goes wrong when prompting the user

// Prompt the player O
do
{
     inputSuccess = true;
     // Your code
     // If anything fails, display an error message and set "inputSuccess" to false
     if(ticTac[positionRow][positionCol] == "*")
     ticTac[positionRow][positionCol] = "O";
     else 
     {
          cout << "That spot has been already chosen!\n\n";
          inputSuccess = false;
     }
     displayBoard(ticTac);
} while (inputSuccess == false); // The loop will continue as long as something goes wrong when prompting the user 


Is that what you want?
That's what I know how to do so yes
So your problem is solved now?
I would suggest simply using a while loop for user-input. The loop should repeatedly prompt the user until he picks a valid positions. This way, we can eliminate the use of the intermediary bool variable.

1
2
3
4
5
6
7
8
9
cout << "Enter a row and column #: ";
cin >> positionRow >> positionCol;

while( ticTac[positionRow][positionCol] == "X" || ticTac[positionRow][positionCol] == "O")
{
          cout << "That position has already been taken! Please enter 
          another row and column: ";
          cin >> positionRow >> positionsCol;
}
Last edited on
I would also suggest setting up a counter in the main while-loop. The counter should be incremented in the loop, and we can use it to determine which player's turn it is. If the counter is even, player X, if odd then player O.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

{
    int positionRow;
    int positionCol;
    int positionRow2;
    int positionCol2;

    cout << "Player X : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    ticTac[positionRow][positionCol] = "X";
    printBoard(ticTac);


    cout << "Player O : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow2;
    cout << "Enter column #: ";
    cin >> positionCol2;




    ticTac[positionRow][positionCol] = "O";

    while( ticTac[positionRow][positionCol] == "X" || ticTac[positionRow][positionCol] == "O")
{
          cout << "That position has already been taken! Please enter another row and column: " << endl;
          cin >> positionRow >> positionCol;
}
    printBoard(ticTac);

    cout << "Player X : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    ticTac[positionRow][positionCol] = "X";
    printBoard(ticTac);

    cout << "Player O : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    ticTac[positionRow][positionCol] = "O";
    printBoard(ticTac);

    cout << "Player X : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    ticTac[positionRow][positionCol] = "X";
    printBoard(ticTac);

    cout << "Player O : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    ticTac[positionRow][positionCol] = "O";
    printBoard(ticTac);
}


Everything is going somewhat good, but the X's and O's aren't showing up with each other when I print the board.

For example it'll only show where X is or O is at one time. Not together.

Also, I understood your while loop.
@Arslan7041
The reason I use "inputSuccess" is that there are actually more than one problem that the program may encounter. So what if an user entered an invalid positionRow or positionCol?

For example :
Enter row : 4
Enter column : 8
Also :
while( ticTac[positionRow][positionCol] == "X" || ticTac[positionRow][positionCol] == "O")

Why not use :
while( ticTac[positionRow][positionCol] != "*")
@OP
Is it your real code? If so, I think you are messing things up again.
Yes!

When I ran the program it prompted the user correctly when they put in the wrong input, but then it printed the board and the X's and O's weren't together.
@Closed-Account: If we are making it that complex, I would suggest making a whole other function called validateInput() which makes sure the input is valid in all cases. Even then, a boolean is not needed, simply add the additional conditions to the while loop.
Last edited on
Ah, so just make a function with one while loop and call it in the userInput function?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>

using namespace std;


const int ROWS=3;
const int COLS=3;

void printBoard(string ticTac[][COLS]);
void userInput(string ticTac[][COLS], int positionRow, int positionCol);
void inputValidation (string ticTac[][COLS], int positionRow, int positionCol);


int main()
{

    int positionRow;
    int positionCol;


    string ticTac[ROWS][COLS]= {"*","*","*",
                                "*", "*","*",
                                "*","*","*"
                               };
    printBoard(ticTac);
    userInput(ticTac, positionRow, positionCol);
    inputValidation(ticTac, positionRow, positionCol);



    return 0;
}

void printBoard(string ticTac[][COLS])
{
    for (int i = 0; i < ROWS; i++)
    {
        cout << '\t';
        for (int j = 0; j < COLS; j++)
        {
            cout << ticTac[i][j] << ' ';
        }
        cout << endl;

    }


}

void userInput(string ticTac[][COLS], int positionRow, int positionCol)
{

    cout << "Player X : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    ticTac[positionRow][positionCol] = "X";
    printBoard(ticTac);


    cout << "Player O : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;




    ticTac[positionRow][positionCol] = "O";

    inputValidation(ticTac, positionRow, positionCol);

    printBoard(ticTac);


    cout << "Player X : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    ticTac[positionRow][positionCol] = "X";

    inputValidation(ticTac, positionRow, positionCol);

    printBoard(ticTac);


    cout << "Player O : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    ticTac[positionRow][positionCol] = "O";

    inputValidation(ticTac, positionRow, positionCol);

    printBoard(ticTac);

    cout << "Player X : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    ticTac[positionRow][positionCol] = "X";

    inputValidation(ticTac, positionRow, positionCol);

    printBoard(ticTac);

    cout << "Player O : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    ticTac[positionRow][positionCol] = "O";

    inputValidation(ticTac, positionRow, positionCol);

    printBoard(ticTac);
}

void inputValidation (string ticTac[COLS], int positionRow, int positionCol)
{
    while(ticTac[positionRow][positionCol] == "X" || ticTac[positionRow][positionCol] == "O")
    {
        cout << "That position has already been taken! Please enter another row and column: " << endl;
        cin >> positionRow >> positionCol;
    }

}


This is what I have so far. Getting these two errors.
https://s32.postimg.org/7zyw13dzp/Screenshot_48.png
Since you are struggling so much, I will give you this as a present :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
bool is_between(int a, int b, int c)
{
     return ((a <= b) && (b < c));
}

bool validateInput(string ticTac[][COLS], int positionRow, int positionCol, string player)
{
     if(!cin)
     {
          cin.clear(); 
          cin.ignore(1000, '\n');
          cout << "Error! Please clarify this error message. (1)" << "\n\n";
          return false;
     }

     if(!is_between(0, positionRow, ROWS) || !is_between(0, positionCol, COLS))
     {
          cout << "Error! Please clarify this error message (2)." << "\n\n";
          return false;
     }

     if(ticTac[positionRow][positionCol] != "*")
     {
          cout << "Error : That spot has already been taken" << "\n\n";
          return false;
     }

     ticTac[positionRow][positionCol] = player;
     displayBoard(ticTac);
     return true;
}

void promptUser(string ticTac[][COLS], string player)
{
     int positionCol, positionRow;
     if(player != "X" && player != "O") player = "X";
     cout << "Player (" << player << ")'s turn : " << endl;
     do
     {
          cout << "enter row : ";
          cin >> positionRow;
           cout << "enter column : ";
          cin >> positionCol;
     } while (validateInput(ticTac, positionRow, positionCol, player) == false);
}

void userInput(string ticTac[][COLS])
{
     bool gameOver = false;
     bool xTurn = true;
     while(gameOver == false)
     {
          if(xTurn) promptUser(ticTac, "X");
          else promptUser(ticTac, "O");
          xTurn = !xTurn;
     }
}
Hi,

You are sending a 2d array to the function, so that needs to be reflected in the first argument.

129
130
131
132
void inputValidation (string ticTac[][COLS], int positionRow, int positionCol)
{
    while(ticTac[positionRow][positionCol] == "X" || ticTac[positionRow][positionCol] == "O")
    {


Btw, there is no need to post pictures of your compiler output, just copy and paste into your post - that is what everyone else does :+)

Also, on lines 17 and 18, both those variables are un-initialised. It might not seem important right now, but one should always initialise your variables at declaration - it's a golden rule. It catches lots of people out, young and old. The way I look at it, if one has compiler warnings then one isn't finished. Warnings are your friend, they help you write better code.
Pages: 1... 56789... 13