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;
}
#include<iostream>
#include<cstdlib>
#include<ctime>
usingnamespace 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') {
}
elseif (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.
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.