Writing Functions for Number guessing game

Trying to finish an assignment and need some assistance. Trying to write functions for a number generator 100-1000. I need to Generate a random number between 100 and 999 by calling function generate_rand_num. I need to Read and validate a guess from the user by calling function read_and_validate, and Ask the user if (s)he wants to play again by calling function play_again.

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
int main()
{
	srand(time(NULL)); 
	// Game instructions and Header
	cout << "************************************************" << endl;
	cout << "*      Guess a number between 100 and 999      *" << endl;
	cout << "************************************************" << endl;
	cout << "" << endl;
	cout << "" << endl;

	while (true) { // Main loop.
		// Defining variables
		int number = (rand() % (999 - 100 + 1)) + 100;
		int guess; 
		int tries = 0; 
		char answer; 

		while (true) { // Get user number loop.
			// Get number.
			// Read number of tries available
			cout << 10 - tries << " tries left!\n";
			cout << "Enter a number between 100 and 999.\n";
			cin >> guess;
			cin.ignore();
			cout << "\n";

			// Check is tries are taken up.
			if (tries >= 10) {
				break;
			}

			// Reading guess from user, and comparing guess
			if (guess < 100 || guess > 999) {
				cout << "Incorrect number! Pick a number between 100 and 999!\n";
				cout << "\n";
				tries--;
			}
			else if (guess > number) {
				cout << "Try lower! Guess again.\n";
			}
			else if (guess < number) {
				cout << "Try higher! Guess again.\n";
			}

			else {
				break;
			}
			tries++;
		}

		// If user runs out of tries
		if (tries >= 10) {
			cout << "You ran out of tries!\n\n";
		}
		else {
			// Or, user won.
			cout << "Congratulations!! " << endl;
			cout << "You got the right number in " << tries << " tries!\n";
		}

		while (true) { // Loop to ask user is he/she would like to play again.
			// Get user response.
			cout << "Would you like to play again (Y/N)? ";
			cin >> answer;
			cin.ignore();

			// Check if proper response.
			if (answer == 'n' || answer == 'N' || answer == 'y' || answer == 'Y') {
				break;
			}
			else {
				cout << "Please enter \'Y\' or \'N\'...\n";
			}
		}

		// Check user's input and run again or exit;
		if (answer == 'n' || answer == 'N') {
			cout << "Thank you for playing!";
			break;
		}
		else {
			std::cout << "\n\n\n";
		}
	}

	// Safely exit.
	cout << "\n\nEnter anything to exit. . . ";
	cin.ignore();
	return 0;
}

Last edited on
That's not a complete program.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Heres the complete program, the functions are confusing when needing to read and validate the reponse and the play again function.
where are you stuck?
there are no functions there, just a monolithic main.

you want something like this, above main is easiest place for it:

int generate_rand_num()
{
return rand()%900+100; //0 to 899 + 100
}

start slow: try putting that in main, and using it.
eg just say
for(int i = 0: i < 10; i++)
cout << generate_rand_num() << endl;
and look at the values it prints.
then you can use it in your real code after checking it out,
say
something = generate_rand_num();
and then use the value and so on.
Last edited on
Re-write the program using functions.
void game_instructions();
//This function should output the game logo/instructions

int generate_rand_num(int min, int max);
//This function should generate and return a random number between min and max

void read_and_validate (int& input, int min, int max);
//This function should read an integer from the user and validate it to ensure it is in the range specified by min and max.
//The function should not return a value. The validated input should be stored in reference parameter input.

bool play_again();
//This function should ask the user if (s)he wants to play again. (User input must be validated.)
//If yes, the function should return true. Otherwise, it should return false.

The output of your program should be exactly the same.
and what issues are you having with functions? You've got the function declarations. Now you need to write the function definitions. This mainly should be taking the code from the existing main() and moving into a function body. Then replacing the removed main() code with a call to the appropriate function.

Try one (the first 2 are probably the easiest) and see how it goes. If you get issues, then post your revised code.

Note that if you use srand()/rand() then srand() should only be called once.
so for the random number function I have this.
1
2
3
4
5
6
void int generate_rand_num(int 100, int 999);
{
	srand((unsigned)time(0));
	int number;
	number = (rand() % (999 - 100 + 1)) + 100;
}

Note that if you use srand()/rand() then srand() should only be called once.


As you could call generate_rand_num() several times if playing more than once, how many times would srand() be called?

How are you returning the generated number to the calling function?? You have a function return type of void int which is invalid - and no return statement.

Also L1 is a function declaration (with ; at the end) and not a function definition (without the ;). You are trying to declare variables 100 and 999 as type int. These are invalid variable names.

Last edited on
Using C++ random - rather than C rand()/srand(), then:

1
2
3
4
5
6
7
8
9
#include <random>

// Other code as needed

int generate_rand_num(int low, int high) {
	static std::mt19937 rng(std::random_device {}());

	return std::uniform_int_distribution<int>(low, high)(rng);
}


and then called as:

 
int number {generate_rand_num(100, 999)};


game instructions is even easier:

1
2
3
4
5
6
7
8
void game_instructions() {
	// Game instructions and Header
	cout << "************************************************" << endl;
	cout << "*      Guess a number between 100 and 999      *" << endl;
	cout << "************************************************" << endl;
	cout << "" << endl;
	cout << "" << endl;
}


and called as:

 
game_instructions();


Note that these are function definitions.

Re-write the program using functions.


As given in that post, those are function declarations. If a function is used before it's function body is given (the definition). then the function needs to be declared so that the compiler knows about the function. The declaration(s) go before main(). The function definitions then then go after main(). If the function definitions go before main() (ie before their use) then the function declarations are not needed.

Using these functions, the code would be:

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
#include <random>
#include <iostream>

using namespace std;

void game_instructions();
int generate_rand_num(int min, int max);

int main() {
	game_instructions();

	while (true) { // Main loop.
		// Defining variables
		int number = generate_rand_num(100, 999);
		int guess;
		int tries = 0;
		char answer;

		while (true) { // Get user number loop.
			// Get number.
			// Read number of tries available
			cout << 10 - tries << " tries left!\n";
			cout << "Enter a number between 100 and 999.\n";
			cin >> guess;
			cin.ignore();
			cout << "\n";

			// Check is tries are taken up.
			if (tries >= 10) {
				break;
			}

			// Reading guess from user, and comparing guess
			if (guess < 100 || guess > 999) {
				cout << "Incorrect number! Pick a number between 100 and 999!\n";
				cout << "\n";
				tries--;
			} else if (guess > number) {
				cout << "Try lower! Guess again.\n";
			} else if (guess < number) {
				cout << "Try higher! Guess again.\n";
			}

			else {
				break;
			}
			tries++;
		}

		// If user runs out of tries
		if (tries >= 10) {
			cout << "You ran out of tries!\n\n";
		} else {
			// Or, user won.
			cout << "Congratulations!! " << endl;
			cout << "You got the right number in " << tries << " tries!\n";
		}

		while (true) { // Loop to ask user is he/she would like to play again.
			// Get user response.
			cout << "Would you like to play again (Y/N)? ";
			cin >> answer;
			cin.ignore();

			// Check if proper response.
			if (answer == 'n' || answer == 'N' || answer == 'y' || answer == 'Y') {
				break;
			} else {
				cout << "Please enter \'Y\' or \'N\'...\n";
			}
		}

		// Check user's input and run again or exit;
		if (answer == 'n' || answer == 'N') {
			cout << "Thank you for playing!";
			break;
		} else {
			std::cout << "\n\n\n";
		}
	}

	// Safely exit.
	cout << "\n\nEnter anything to exit. . . ";
	cin.ignore();
	return 0;
}

void game_instructions() {
	// Game instructions and Header
	cout << "************************************************" << endl;
	cout << "*      Guess a number between 100 and 999      *" << endl;
	cout << "************************************************" << endl;
	cout << "" << endl;
	cout << "" << endl;
}

int generate_rand_num(int low, int high) {
	static std::mt19937 rng(std::random_device {}());

	return std::uniform_int_distribution<int>(low, high)(rng);
}

Last edited on
Topic archived. No new replies allowed.