How to

Hello. I have done seample Tic Tac Toe game. But i want to replay without exiting the progam. I made bool function witch ask u do u want to play again or want to exit the program


1
2
3
4
5
6
7
8
9
10
11
bool Replay(){
		char answer;
		cout << "if u want play again press Y else N" << endl;
		cin >> answer;
		if(answer == 'Y'){
			//code
		}
		else if (answer == 'N'){
			break;
		}
	}

but it doesn't work. break is error here and i have no idea what i must to write in case of Y. help me please.
Last edited on
You are not in a loop. Breaks only work with loops and switch statements.
Your function nees to return a bool.

1
2
3
4
5
6
7
bool Replay() {
	char answer;
	cout << "if u want play again press Y else N" << endl;
	cin >> answer;

        return (answer == 'Y');
}
oh yeah. But while user press on N how i can exit the program?
This will make it quit on the user entering N:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

bool Replay() {
	char answer;
	std::cout << "if u want play again press Y else N" << std::endl;
	std::cin >> answer;

        return (answer != 'N');
}

int main()
{
    do {

       std::cout << "[Game being played]" << std::endl;
       std::cout << "You [won/lost]!" << std::endl;

    } while (Replay());

    std::cout << "Bye!" << std::endl;
}
Last edited on
if answer is 'n' replay() return false ( or 0 if you want ) then:

1
2
3
4
5
6
7
8
9
10


if( Replay() )
{
     // code
}
else
      return 0 ; // example...




// submitted on retard. sorry
Last edited on
nope. it's dont work :(

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
bool Replay(){
		char answer;
		cout << "if u want play again press y else n ";
		cin >> answer;
		return (answer == 'Y');
	}

int main(int argc, char** argv) {
	
	
	do { 
	
	Game MyGame;
	MyGame.Drow();
	while(true){
		MyGame.Input();
		MyGame.Drow();
		if(MyGame.Logic() == 'X'){
			cout << "Congratulations, X won!" << endl;
			break;
		}
		else if(MyGame.Logic() == 'O'){
			cout << "Congratulations, O won!" << endl;
			break;
		}
		MyGame.PlayerChange();
	}
	}while(Replay());
	
	system("pause");
	return 0;
}


when i press n program dont exit
can you display the output dialog with console ( after MyGame.Drow() ;) ?
Works for me... the problem might be parts of the code you're not showing?

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

#include <iostream>
using namespace std;

bool Replay() {
		char answer;
		cout << "if u want play again press y else n ";
		cin >> answer;
		return (answer == 'Y');
	}

int main(int argc, char** argv) {
	
	do { 

		while(true) {

			std::cout << "test...";
			break;
		}
	
	} while(Replay());
	
	std::cout << "bye" << std::endl;

	return 0;
}


Note: you can #include <cctype> and do tolower(answer) to let both y and Y be valid input.

1
2
3
4
5
6
7
8
9
10
11
12

#include <iostream>
#include <cctype>
using namespace std;

bool Replay() {
    
	char answer;
	cout << "if u want play again press y else n ";
	cin >> answer;
	return (tolower(answer) == 'y');
}


The above will make 'Y' or 'y' return true, anything else return false.
Last edited on
So....

here is my problematic code:

1
2
3
4
5
6
7
8
9
10
11
bool Replay(){
		char answer;
		cout << "if u want play again press y else n ";
		cin >> answer;
		if (answer == 'Y'){
			Game.NewGame();
		}
		else if (answer == 'N'){
			return 0;
		}
	}


and here is full 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
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
138
139
#include <iostream>

using namespace std;

class Game {
	private:
		char square[10] = {'0','1','2','3','4','5','6','7','8','9'};
		char player = 'X';
	public:
		void Drow(){
		system("cls");
		cout << "         Tic Tac Toe" << endl << endl;
		cout << "Player 1 (X)  -  Player 2 (O)" << endl << endl;
		cout << endl;
		cout << "     |     |     " << endl;
		cout << "  " << square[7] << "  |  " << square[8] << "  |  " << square[9] << endl;
		cout << "_____|_____|_____" << endl;
		cout << "     |     |     " << endl;
		cout << "  " << square[4] << "  |  " << square[5] << "  |  " << square[6] << endl;
		cout << "_____|_____|_____" << endl;
		cout << "     |     |     " << endl;
		cout << "  " << square[1] << "  |  " << square[2] << "  |  " << square[3] << endl;
		cout << "     |     |     " << endl << endl;
		}

		//contructor
		Game(){
		NewGame();
		}

		//New game
		void NewGame(){
		
		}
		
		void PlayerChange(){
		if(player ==  'X')
		   player = 'O';
		else
			player = 'X';
		}
		
		void Input(){
		int var;
		cout << "Press the Number of the Field:";
		cin >> var;
		
		if(square[var] !='X' && square[var] !='O'){
			for(int i=0; i<=9; i++){
				if(var == i){
					square[i] = player;
					}
				}
			}else{
				cout << "Square" << var << "is used! Please input another Square!" << endl;
				system("pause");
				PlayerChange();
			}
		}
		char Logic(){
		//player X
		//rows
		if(square[1] == 'X' && square[2] == 'X' && square[3] == 'X')
		return 'X';
		if(square[4] == 'X' && square[5] == 'X' && square[6] == 'X')
		return 'X';
		if(square[7] == 'X' && square[8] == 'X' && square[9] == 'X')
		return 'X';
		//colums
		if(square[1] == 'X' && square[4] == 'X' && square[7] == 'X')
		return 'X';
		if(square[2] == 'X' && square[5] == 'X' && square[8] == 'X')
		return 'X';
		if(square[3] == 'X' && square[6] == 'X' && square[9] == 'X')
		return 'X';
		///diagonal
		if(square[1] == 'X' && square[5] == 'X' && square[9] == 'X')
		return 'X';
		if(square[3] == 'X' && square[5] == 'X' && square[7] == 'X')
		return 'X';
		
		//player O
		//rows
		if(square[1] == 'O' && square[2] == 'O' && square[3] == 'O')
		return 'O';
		if(square[4] == 'O' && square[5] == 'O' && square[6] == 'O')
		return 'O';
		if(square[7] == 'O' && square[8] == 'O' && square[9] == 'O')
		return 'O';
		//colums
		if(square[1] == 'O' && square[4] == 'O' && square[7] == 'O')
		return 'O';
		if(square[2] == 'O' && square[5] == 'O' && square[8] == 'O')
		return 'O';
		if(square[3] == 'O' && square[6] == 'O' && square[9] == 'O')
		return 'O';
		///diagonal
		if(square[1] == 'O' && square[5] == 'O' && square[9] == 'O')
		return 'O';
		if(square[3] == 'O' && square[5] == 'O' && square[7] == 'O')
		return 'O';
		
		return ' ';	
	}
};
	
	bool Replay(){
		char answer;
		cout << "if u want play again press y else n ";
		cin >> answer;
		if (answer == 'Y'){
			Game.NewGame();
		}
		else if (answer == 'N'){
			return 0;
		}
	}

int main(int argc, char** argv) {
	
	Game MyGame;
	MyGame.Drow();
	while(true){
		MyGame.Input();
		MyGame.Drow();
		if(MyGame.Logic() == 'X'){
			cout << "Congratulations, X won!" << endl;
			break;
		}
		else if(MyGame.Logic() == 'O'){
			cout << "Congratulations, O won!" << endl;
			break;
		}
		MyGame.PlayerChange();
	}

	system("pause");
	return 0;
}
line 107:
bool replay() .... your compiler accept this function?
RanGH, you should turn on warnings for your compiler.
In g++ or clang, you can use -Wall

In Visual Studio, I guess read this https://msdn.microsoft.com/en-us/library/23k5d385.aspx

As ar2007 mentioned, your replay function is not compiling.

 In function 'bool Replay()':
112:8: error: expected unqualified-id before '.' token
117:2: warning: control reaches end of non-void function [-Wreturn-type]
The error, translated: Game is a class, not an object. You can't call Game.NewGame(). You need an instance of your Game class (MyGame), like you have in main.

The warning, translated: The end of your function can be reached without having a return statement. A non-void function must return a value.

By the fact that you try to return 0, it seems to me you aren't completely understanding what a bool is. A bool can have two values: true or false. 0 happens to translate to false, but I just want you to be aware of that.

Also, even if your Replay function did compile, now you're not calling it from anywhere.
Last edited on
Like this...
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>

using namespace std;

class Game {
private:
	char square[10] = { '0','1','2','3','4','5','6','7','8','9' };
	char player = 'X';
public:
	void Drow() {
		system("cls");
		cout << "         Tic Tac Toe" << endl << endl;
		cout << "Player 1 (X)  -  Player 2 (O)" << endl << endl;
		cout << endl;
		cout << "     |     |     " << endl;
		cout << "  " << square[7] << "  |  " << square[8] << "  |  " << square[9] << endl;
		cout << "_____|_____|_____" << endl;
		cout << "     |     |     " << endl;
		cout << "  " << square[4] << "  |  " << square[5] << "  |  " << square[6] << endl;
		cout << "_____|_____|_____" << endl;
		cout << "     |     |     " << endl;
		cout << "  " << square[1] << "  |  " << square[2] << "  |  " << square[3] << endl;
		cout << "     |     |     " << endl << endl;
	}
	void NewGame();
	/*contructor
	Game() {
		NewGame();*/


	//New game
	

	void PlayerChange() {
		if (player == 'X')
			player = 'O';
		else
			player = 'X';
	}

	void Input() {
		int var;
		cout << "Press the Number of the Field:";
		cin >> var;

		if (square[var] != 'X' && square[var] != 'O') {
			for (int i = 0; i <= 9; i++) {
				if (var == i) {
					square[i] = player;
				}
			}
		}
		else {
			cout << "Square" << var << "is used! Please input another Square!" << endl;
			system("pause");
			PlayerChange();
		}
	}
	char Logic() {
		//player X
		//rows
		if (square[1] == 'X' && square[2] == 'X' && square[3] == 'X')
			return 'X';
		if (square[4] == 'X' && square[5] == 'X' && square[6] == 'X')
			return 'X';
		if (square[7] == 'X' && square[8] == 'X' && square[9] == 'X')
			return 'X';
		//colums
		if (square[1] == 'X' && square[4] == 'X' && square[7] == 'X')
			return 'X';
		if (square[2] == 'X' && square[5] == 'X' && square[8] == 'X')
			return 'X';
		if (square[3] == 'X' && square[6] == 'X' && square[9] == 'X')
			return 'X';
		///diagonal
		if (square[1] == 'X' && square[5] == 'X' && square[9] == 'X')
			return 'X';
		if (square[3] == 'X' && square[5] == 'X' && square[7] == 'X')
			return 'X';

		//player O
		//rows
		if (square[1] == 'O' && square[2] == 'O' && square[3] == 'O')
			return 'O';
		if (square[4] == 'O' && square[5] == 'O' && square[6] == 'O')
			return 'O';
		if (square[7] == 'O' && square[8] == 'O' && square[9] == 'O')
			return 'O';
		//colums
		if (square[1] == 'O' && square[4] == 'O' && square[7] == 'O')
			return 'O';
		if (square[2] == 'O' && square[5] == 'O' && square[8] == 'O')
			return 'O';
		if (square[3] == 'O' && square[6] == 'O' && square[9] == 'O')
			return 'O';
		///diagonal
		if (square[1] == 'O' && square[5] == 'O' && square[9] == 'O')
			return 'O';
		if (square[3] == 'O' && square[5] == 'O' && square[7] == 'O')
			return 'O';

		return ' ';
	}
};

void Game::NewGame() {
	player = 'X';
	square[0] = '0';
	square[1] = '1';
	square[2] = '2';
	square[3] = '3';
	square[4] = '4';
	square[5] = '5';
	square[6] = '6';
	square[7] = '7';
	square[8] = '8';
	square[9] = '9';
	Drow();
}

bool Replay(Game &MyGame) {
	char answer;
	cout << "if u want play again press y else n ";
	cin >> answer;
	if (answer == 'Y' || answer == 'y') {
		MyGame.NewGame();
		return 0;
	}
	else {
		return 1;
	}
}

int main(int argc, char** argv) {

	Game MyGame;
	MyGame.Drow();
	while (true) {
		MyGame.Input();
		MyGame.Drow();
		if (MyGame.Logic() == ' ') {
			MyGame.PlayerChange();
		}
		if (MyGame.Logic() == 'X') {
			cout << "Congratulations, X won!" << endl;
			if (Replay(MyGame)) break;
		}
		else if (MyGame.Logic() == 'O') {
			cout << "Congratulations, O won!" << endl;
			if (Replay(MyGame)) break;
		}
	}

	system("pause");
	return 0;
}

Last edited on
thank u guys.
@Manga can u explain me why u use like this :
bool Replay(Game &MyGame)
now its works perfectly!
I wanted to call the NewGame() function but MyGame.Newgame() was out of scope. I had to pass by refrence the MyGame class into the function Replay to bring it into scope. Then I could make the function call MyGame.NewGame();
Oh yah... thank you
Topic archived. No new replies allowed.