Recursion and Return Values [Help]

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?

Thank you in advance.

The BashPotato

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <ctime>

using namespace std;

const int MAX = 10;
const int MIN = 0;
const char YES = 'Y';
const char 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.
Last edited on
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <ctime>

const int MAX = 10;
const int MIN = 0;
const char YES = 'Y';
const char 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
    const int 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 ) {

        const int 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.
Last edited on
I am aware I could just recursively call main() and be done with it

Actually you can't. Calling main() from any function, including main(), is forbidden in a standard conforming C++ program.
Topic archived. No new replies allowed.