Prompt Player to Play again or Quit

I am wanting to provide the player of my game with an option to play the game again once the end screen has appeared or to quit the application(tic-tac-toe game) as alls I currently have is once someone has won the application closes.

This is what I currently have:

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
  	while (1)
	{
		b++;
		Input();
		Display();
		//win function if player has won
		if (Win() == 'X')
		{
			system("cls");
			cout << "X has Won!" << endl;
			break;
		}
		else if (Win() == 'O')
		{
			system("cls");
			cout << "O has Won!" << endl;	
			break;
		}
		//if players tie function
		else if (Win() == '/' && b == 9)
		{
			system("cls");
			cout << "It's a Tie!" << endl;
			break;
		}
		TogglePlayer();
	}
What do I put in in order to go back to the start of the game, if player inputs continue(or Play in my case)?
Last edited on
The easiest way to do this (I think) is to put your hole program (under the line with main()) in a do...while loop. It's just like a while loop but it completes the code 1 time before checking the conditions

Int main ()

do

Your code here....

While (x<5)



This will do your cone one time, than if x is greater than 5 will loop back to "do". Knowing this you should be able to use the do....while loop for replaying your game.
So where about would I put do and the While(x<5) here
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
int main()
{
	
	Names();
	b = 0;
	Display();
	//loop that will run until a player has won or tied
	while (1)
	{
		b++;
		Input();
		Display();
		//win function if player has won
		if (Win() == 'X')
		{
			system("cls");
			cout << X << " has Won!" << endl;
			cout << "Would you like to Play again? or Quit?" << endl;
			cout << "'Play' to play again, or 'Quit' to Quit." << endl;
			string choice2;
			cin >> choice2;
			if (choice2 == "Play")
			{
				/*go to start*/
			}
			else if (choice2 == "Quit")
			{
				return 0;
			}
			
		}
		else if (Win() == 'O')
		{
			system("cls");
			cout << O << " has Won!" << endl;	
			cout << "Would you like to Play again? or Quit?" << endl;
			cout << "'Play' to play again, or 'Quit' to Quit." << endl;
			string choice2;
			cin >> choice2;
			if (choice2 == "Play")
			{
				/*go to start*/
			}
			else if (choice2 == "Quit")
			{
				return 0;
			}
			
		}
		//if players tie function
		else if (Win() == '/' && b == 9)
		{
			system("cls");
			cout << "It's a Tie!" << endl;
			cout << "Would you like to Play again? or Quit?" << endl;
			cout << "'Play' to play again, or 'Quit' to Quit:";
			string choice2; 
			cin >> choice2;
			if (choice2 == "Play")
			{
				/*go to start*/
			}
			else if (choice2 == "Quit")
			{
				return 0;
			}
			
		}
		TogglePlayer();
	}

	system("PAUSE");

	return 0;
}
where are you getting this code? this doesn't look rite to me at all. you need to specify an output type for your functions. I can't really work out your code, but i'll provide a simple program that demonstrates a do....while loop.

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
#include <iostream>
using namespace std;

int Umain = 0;
double Atemp = 0;
double Utemp = 0;
double Working = 0;
double Total = 0;
char Answer = 'x';

	main()
{
	do
	{
	cout << "How many numbers are there? :";
	cin >> Umain;
	cout << "\n";
		for (Utemp = Umain; Utemp > 0; Utemp--)
		{
			cout << "Please enter a number. :";
			cin >> Atemp;
			Working = (Working + Atemp);			
		}
		
	Total = (Working / Umain);
	
	cout << "Your Average is " << Total << "\n";
	cout << "Want another average?";
	cin >> Answer;
	}
	
	while (Answer == 'y');
	
	cout << "Thanks for using my program.";
	return 0;


	
}
Games usually use while loops and seldom use do{}while();. The best way I know of is to make a bool variable (from looking at the code).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
      bool quit = false;
      while (!quit){
                // play game
                if (Win() == 'X'){ // same for other conditions
                      char choice;
                      cout << "Would you like to play again? (y/n) ";
                      cin >> choice;
                      if(choice =='y'){
                                // clear the board and go again
                      }else{ // player said no and want to quit
                          quit = true;
                      }
                 }
        }
        return 0;
}


Sample code that I wrote and tested:
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
#include <iostream>
using namespace std;

int main()
{
	// doing counting just to show
	bool quit = false;
	int count = 0;
	char choice;
	while (!quit){
		count++;
		cout << "Count is : " << count << endl
		     << "Want to go again? (y/n)";
		cin >> choice;
		if(choice == 'y'){
			cout << "If this was a complex game we would reset everything "
			     << "to a clean board and begin agian.\n";
		}else{ // they don't want to play anymore
			cout << "Well good-bye!\n";
			quit = true;
		}
	}
	cout << "Hope you enjoyed the game!\n";
	return 0;
}
Count is : 1
Want to go again? (y/n)y
If this was a complex game we would reset everything to a clean board and begin agian.
Count is : 2
Want to go again? (y/n)y
If this was a complex game we would reset everything to a clean board and begin agian.
Count is : 3
Want to go again? (y/n)y
If this was a complex game we would reset everything to a clean board and begin agian.
Count is : 4
Want to go again? (y/n)n
Well good-bye!
Hope you enjoyed the game!
Press any key to continue . . .
closed account (1vRz3TCk)
WheatFieldOnFire wrote:
OR, the ugly way (people don't like this) is to call main function again,....
BAD ADVICE

Never call main(), it's your job to define main but you never call it.
OR, the ugly way (people don't like this) is to call main function again

That's not ugly - it's illegal C++.
I did it before, and it worked.

Doing so results in undefined behavior. For the same reason we don't recommend dereferencing pointers to random memory locations we shouldn't recommend recursively calling main, even though sometimes it appears to work.
> I did it before, and it worked.

Calling main is allowed in C; if it was a C program, it would work.
http://coliru.stacked-crooked.com/a/b7dae4f9e6b249a7

Many C++ compilers with their default settings compile a language that is close to, but is not standard C++.
So it might have compiled cleanly if conformance to C++ was not specifically asked for.
For instance, clang++, g++ without -pedantic-errors:
http://coliru.stacked-crooked.com/a/fa75618787da0ffd

C++ does not allow main() (in the global namespace) to be called.
For instance, clang++, g++ with -pedantic-errors:
http://coliru.stacked-crooked.com/a/c8ade95e55a5a73e

-pedantic-errors asks the compiler to strictly conform to C++ to the extent that it can.
Topic archived. No new replies allowed.