How to repeat the program through input

Hello, I have been struggling with this for quite some time now and are needing help in how to repeat my program through the appropriate input. I am unsure on what I am to put in in order for it to repeat.

Here is my current main:
I am wanting to repeat the program once someone inputs 'Y' or 'y'

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 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, Y/N." << endl;
			string choice2;
			cin >> choice2;
			if (choice2 == "Y" || "y")
			{
				/*Repeat the program*/
			}
			else if (choice2 == "N" || "n")
			{
				return 0;
			}
			
		}
		else if (Win() == 'O')
		{
			system("cls");
			cout << O << " has Won!" << endl;
			cout << "Would you like to Play again, Y/N." << endl;
			string choice2;
			cin >> choice2;
			if (choice2 == "Y" || "y")
			{
				/*Repeat the program*/
			}
			else if (choice2 == "N" || "n")
			{
				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, Y/N." << endl;
			string choice2;
			cin >> choice2;
			if (choice2 == "Y" || "y")
			{
				/*Repeat the program*/
			}
			else if (choice2 == "N" || "n")
			{
				return 0;
			}
			
		}
		TogglePlayer();
	}

	system("PAUSE");

	return 0;
}
Last edited on
You probably just need to change your while loop

from while (1) to

while (PlayAgain==0)

then
1
2
3
			if (choice2 == "Y" || "y")
			{
				PlayAgain=0; /*Repeat the program*/

if (choice2 == "Y" || "y")
Each condition must be typed in full:
if (choice2 == "Y" || choice2 == "y")

The original version will always be true. Why? A zero value is considered as logical false, and any non-zero as logical true. Hence the first version is the same as if ((choice2 == "Y") || (true) ) and anything || true evaluates as true.

Usually for a single-letter response such as 'y' or 'n' type char would be used rather than string. Remember to use single quotes for character literals 'Y' and 'y' etc. if you do that.
Last edited on
Topic archived. No new replies allowed.