Hello everyone, I am having some difficulty figuring out how to restart this program or "Loop it over and over" as long as the user wants to play again.
Before I go any farther, this is a homework assignment, I read the rules and am aware I shouldn't ask for help on this. I am asking because this is the bonus challenge. The program as it sits meets all necessary criteria. The Bonus is to loop the program with recursion, I am only allowed to use recursion. I am aware I could just recursively call main() and be done with it, since the challenge does not state that I can't. How ever we all know the stack builds up every time main is called. And I want to know how to do this properly.
What I have tried is calling wantsToPLay() the program does restart and gives me the prompt inside the wantsToPlay sub routine, however when wantsToPlay is called recursively, the return value does not return to the same area in main as it does on the first run, it simply drops to the bottom of main, prints out my goodbye message and return 0; That is what I don't understand. I thought I understood returns and recursion, but now I feel so lost. How do I return a value to the top part of the main function?
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <ctime>
usingnamespace std;
constint MAX = 10;
constint MIN = 0;
constchar YES = 'Y';
constchar NO = 'N';
void displayOverview();
char wantToPlay();
int getIntValue();
bool play(int userValue);
int main(){
displayOverview();
//set up variables for user entry
char playOrNot = wantToPlay();
int input = -1;
//initialize random seed
srand(time(NULL));
//handle whether or not they want to play
switch(playOrNot){
case YES:
//play
input = getIntValue();
if(play(input)){
cout << "\nwow! You're awesome!" << endl;
}else{
cout << "bummer, you missed" << endl;
}
break;
case NO:
//say bye
cout << "sorry to hear you didn't want to play my game" << endl;
break;
default:
cout << "That was not a valid choice" << endl;
//should never happen
}
cout << "Thanks for playing, Good-Bye" << endl;
return 0;
}
/*
outputs a description of the program to the user
*/
void displayOverview(){
cout << "A simple number guessing game" << endl;
}
/*
prompts user to enter a Y for yes, N for no. uses recursion to validate, and should
only return Y or N
*/
char wantToPlay(){
char answer;
cout << "\nType (Y) to play or (N) to quit: ";
cin >> answer;
answer = toupper(answer);
if (answer == YES || answer == NO) {
return answer;
}
else {
cout << "\nSorry " << answer << " is invalid." << endl;
return wantToPlay();
}
}
/*
prompts the user to enter a number between 1 and 10
uses recursion to validate, and should only return a valid input
*/
int getIntValue(){
int userValue;
cout << "\nEnter a natural number betweem 1 and 10: ";
cin >> userValue;
if (userValue >= MIN && userValue <= MAX) {
return userValue;
}else {
cout << "\nSorry " << userValue << " is invalid." << endl;
return getIntValue();
}
}
/*
generates a random number between 1 and 10, and compares the user's choice, in userValue
to the generated number. Returns true on match, false otherwise
*/
bool play(int userValue){
bool match = false;
//generate a random number
int randomNum = (rand() % MAX) + 1;
cout << "the random number generator created...... "
<< randomNum << endl;
if(userValue == randomNum){
match = true;
}
return match;
}
You might need to post the requirements of your program to get help. It is much harder to rework your program without knowing the specific requirements.
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <ctime>
constint MAX = 10;
constint MIN = 0;
constchar YES = 'Y';
constchar NO = 'N';
void displayOverview();
char wantToPlay();
int getIntValue();
bool play(int userValue);
void keep_playing() ; // keep playing as long as the user wants to play.int main() {
displayOverview();
//initialize random seed
std::srand( std::time(nullptr) );
keep_playing() ;
std::cout << "Thanks for playing, Good-Bye" << '\n';
}
/*
outputs a description of the program to the user
*/
void displayOverview(){
std::cout << "A simple number guessing game" << '\n';
}
/*
prompts user to enter a Y for yes, N for no. uses recursion to validate, and should
only return Y or N
*/
char wantToPlay(){
char answer;
std::cout << "\nType (Y) to play or (N) to quit: ";
std::cin >> answer;
answer = std::toupper(answer);
if (answer == YES || answer == NO) return answer;
std::cout << "\nSorry " << answer << " is invalid." << '\n';
return wantToPlay();
}
/*
prompts the user to enter a number between 1 and 10
uses recursion to validate, and should only return a valid input
*/
int getIntValue() {
int userValue;
std::cout << "\nEnter a natural number betweem 1 and 10: ";
std::cin >> userValue;
if (userValue >= MIN && userValue <= MAX) return userValue;
else {
std::cout << "\nSorry " << userValue << " is invalid." << '\n';
return getIntValue();
}
}
/*
generates a random number between 1 and 10, and compares the user's choice, in userValue
to the generated number. Returns true on match, false otherwise
*/
bool play(int userValue){
//generate a random number
constint randomNum = (rand() % MAX) + 1;
std::cout << "the random number generator created...... "
<< randomNum << '\n';
return userValue == randomNum ;
}
// keep playing till the return value from wantToPlay is not YES
void keep_playing() {
if( wantToPlay() == YES ) {
constint input = getIntValue();
if( play(input) ) std::cout << "\nwow! You're awesome!" << '\n';
else std::cout << "bummer, you missed" << '\n';
keep_playing() ; // recursive call to play once more
}
}
Thank you so much JLBorges, I figured I was doing it the hard way with the case statements. Your over all solution is much more elegant than what I had in mind. I notice you don't use namespace std; I guess I have some reading to do on that. Thank you again, I learned so much from this.