How to jump to a function once a if statement is met?

Hi, I don't know exactly how to put it into words, but once my statement is met in one of my functions I am wanting it to move onto other functions and stop the current one until it is called for again, so far I have had no luck in doing this and am hoping someone else does.
Here is my code that I am struggling with:

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
void playAgain()
{
	string choice2;

	cout << "Would you like to Play again, y/n." << endl;
	cout << "Enter here: ";
	while (1)
	{
		cin >> choice2;
		if (choice2 == "y" || choice2 == "Y")
		{
			system("cls");
			resetGrid();
			Display();
		}
		else if (choice2 == "n" || choice2 == "N")
		{
			exit(0);
		}
		else
		{
			cout << "That is not the correct input, please try again." << endl;
			playAgain();
		}
	}
}

I am not entirely sure whether the code is correct or not but to give you the run down so you can better understand I am wanting this function playAgain() to execute once a player has selected to play again at the end of the game, the first run of the game runs as normal but once this playAgain() function comes into it the game does not work as it should do, whilst entering the field numbers for the game it simply passes to the next lane and displays the playAgain() function which is not what I am wanting.
Here is also my main function if it helps:
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
int main()
{
	system("cls");
	Menu();
	Names();
	b = 0;
	Display();
	//loop that will run until a player has won or tied
	while (1)
	{
		b++;
		Input();
		Display();
		//if player wins/tie function
		//if X (Player 1) wins
		if (Win() == 'X')
		{
			system("cls");
			cout << "------------------" << endl;
			cout << X << " has Won!" << endl;
			cout << "------------------" << endl;
			playAgain();
		}
		//if O (Player 2) wins
		else if (Win() == 'O')
		{
			system("cls");
			cout << "------------------" << endl;
			cout << O << " has Won!" << endl;
			cout << "------------------" << endl;
			playAgain();
		}
		//if players tie
		else if (Win() == '/' && b == 9)
		{
			system("cls");
			cout << "------------------" << endl;
			cout << "It's a Tie!" << endl;
			cout << "------------------" << endl;
			playAgain();
		}
		//change players turns
		else
		{
			TogglePlayer();
		}
	}
}
Last edited on
You have some functions mentioned in your program (resetGrid(), Display() etc) whose definition does not appear in the code posted, so I could not replicate your results but in general what you're looking for is called a break statement - it is a statment that breaks the program out of a current loop (in your case an if loop) if certain conditions are met and the program then proceeds to execute the subsequent code. You can search on-line how to practice it in actual programs or post a small, self-contained program here
Topic archived. No new replies allowed.