Need to break out of the IF statement

I need the code to go back to the beginning of the program after the User inputs Y in the IF statement. Every thing else in the code works fine I just cannot for the life of me figure out how to make it loop back to the beginning. Please help I am so confused. I have been working on for weeks. I am new to this forum so forgive me if I am not clear enough. The instructions for the game are below.

/*-----------------------------------------------------------------------------------
1. The program asks for input until a valid choice (4-20) is given by the user
2. The program asks if the user wants to play again after each round (y/n)
3. if yes (y), the user is asked to choose a number of sides again
4. if no (n), the program gives a thank you message and instructs the user to press [RETURN] to exit (and waits for [RETURN] before exiting)
--------------------------------------------------------------------------------------*/

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;


void chooseNumbers(int num, int roll, char answer);
void playagain(char answer, int num, int roll);


int main() {
int num; //integer for number
int roll; //integer for roll
srand(time(0)); //seed random number generator
char answer;

cout << "You are about to roll a single die" << endl;
cout << "How many sided die would you like to roll(4-20)? ";
cin >> num;
cin.ignore();

roll = 1 + rand() % num; //mods random number

//If you get wrong answer
chooseNumbers(num, roll, answer);

//Play again
playagain(answer, num, roll);


return 0;
}


void chooseNumbers(int num, int roll, char answer) {

while (num < 4 || num > 20){
cout << "Please play again and enter a number between 4 and 20" << endl;
cin >> num;
}

cout << "You rolled: " << endl;
cout << roll << endl;

}

void playagain(char answer, int num, int roll)
{
while (answer != 'Y' && answer != 'N') {

cout << "Do you want to play again? Type in Y for yes, or N for no: " << endl;
cin >> answer;
}

if (answer == 'Y') {

}

else if (answer == 'N') {

cout << "Thank you! Please press RETURN to exit." << endl;
cin.ignore();
cin.get();
cout << "Exiting..." << endl;
}
}


I wish someone would help me. I've asked everywhere :(
Make sure you pay attention to this when you post.
https://www.cplusplus.com/articles/jEywvCM9/
Most forums have similar requirements when it comes to posting code.

So you present code like this, not an unformatted mess.
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
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;


void chooseNumbers(int num, int roll, char answer);
void playagain(char answer, int num, int roll);


int main()
{
  int num;                      //integer for number
  int roll;                     //integer for roll
  srand(time(0));               //seed random number generator
  char answer;

  cout << "You are about to roll a single die" << endl;
  cout << "How many sided die would you like to roll(4-20)? ";
  cin >> num;
  cin.ignore();

  roll = 1 + rand() % num;      //mods random number

  //If you get wrong answer
  chooseNumbers(num, roll, answer);

  //Play again
  playagain(answer, num, roll);


  return 0;
}


void chooseNumbers(int num, int roll, char answer)
{

  while (num < 4 || num > 20) {
    cout << "Please play again and enter a number between 4 and 20" << endl;
    cin >> num;
  }

  cout << "You rolled: " << endl;
  cout << roll << endl;

}

void playagain(char answer, int num, int roll)
{
  while (answer != 'Y' && answer != 'N') {

    cout << "Do you want to play again? Type in Y for yes, or N for no: " << endl;
    cin >> answer;
  }

  if (answer == 'Y') {

  }

  else if (answer == 'N') {

    cout << "Thank you! Please press RETURN to exit." << endl;
    cin.ignore();
    cin.get();
    cout << "Exiting..." << endl;
  }
}


An answer to your question would be to make playagain return a result.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// your parameters were useless
bool playagain(void)
{
  char answer;
  while (answer != 'Y' && answer != 'N') {

    cout << "Do you want to play again? Type in Y for yes, or N for no: " << endl;
    cin >> answer;
  }

  if (answer == 'Y') {
    return true;
  }

  else if (answer == 'N') {

    cout << "Thank you! Please press RETURN to exit." << endl;
    cin.ignore();
    cin.get();
    cout << "Exiting..." << endl;
    return false;
  }
}


Then in main, you can do
1
2
3
do {
  // the game
} while( playagain() );

OK thank you so much. Sorry I am just learning how to program and I do not know what I am doing clearly lol. I have not learned how to use bool functions yet but I will try it out. Thank you.
Topic archived. No new replies allowed.