Tic Tac Toe Program help


I do not get why every time this code prints two enter box : enter box: two time after the first loop. It has something to do with the cin.get but I am not sure how to fix this problem. Thank You
Last edited on
your problem is in getMove()

when you type a char and hit enter, cin gets the keystroke of the char, then comes back and gets the keystroke of the enter. I think. I might be, and probably am wrong about that so someone with a brain they need to flex should flex it in my direction.

Anyway...

rather than using
1
2
char c;
cin.get(c);


use this
1
2
char c;
cin >> c;
Last edited on
The problem is that my teacher entered cin.get so i dont think i am suppose to change it
After cin.get

do cin.ignore(numeric_limits<int>::max(), '\n'); and be sure to #include<limits> .

If you input just one character, you still press enter, so a newline is left in the stream. So, in the next call to getMove(), cin.get takes the newline and continues without giving you a chance to enter anything. Calling cin.ignore (as above) will go through the stream and remove the character up to and including the first newline. Passing in numeric_limits<int>::max() as a parameter tells it to go really far into the stream (in case the user entered a lot of characters).
treepig why did you remove the source code? One of the best parts of the forums is that people can look back in time at other peoples problems to figure out there own.

Here's the code:

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
/**
Lab 4 - Due 7/22/2010.

Convert Lab 3 to a class

1. Implement displayBoard to display Tic Tac Toe board.
2. Prompt User for a box on the board to select, i.e. a number between 1 and 9 with 1 being the upper left corner.

use cin.get(box) to get the box number and isdigit to verify it is a
number;
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
If the box is available put the appropriate X or O in there and switch players, i.e. X becomes O and vice versa.
If the box is NOT available warn the user and get another box until they select a valid open box.

3. After all spots have been select Display "Game Over!";
4. Write a main function to use the TicTacToe class and test all of the above functionality.

**/
#include<iostream>

using namespace std;


class TicTacToe {
public:
	void displayBoard();
	void getMove();
	void playGame();
private:
	char board[9];
	char player; // Switch after each move.
};

void TicTacToe::playGame()
{
    displayBoard();
    
    getMove();
        
	// Your implementation here..
    
}

void TicTacToe::displayBoard()
{
	bool b = 0;
	// Your implementation here...
    for (int i = 0; i < 9; ++i)
	{
		if ( (i+1) % 3 == 0 )
		{
            cout << i + 1;
            
			cout << board[i] << endl;
        }
		else 
		{
            cout << i + 1;
            
			cout << board[i] <<	" | ";
        }

	}

    
}

void TicTacToe::getMove()
{

	cout << "Enter Box: ";
	char c;
	cin.get(c);
        // uncomment the next line to fix the OP's problem
	//cin.ignore( );
	if (c > '9' || c < '0')
		// Error message here.
        
        int number = c - '0';
    
	cout << "your number is " << 23 << endl;
	// Your implementation here...
}

int main()
{
	TicTacToe ttt;
    
    for (int i = 0; i < 9; i++) {
		ttt.playGame();
    }
}
Last edited on
Topic archived. No new replies allowed.