Number Guessing Game Using Void and For Loop

Hello,

Beginner C++ student here, first ever programming class. I am studying the code below for a number guessing game using a for loop and void function. However, I know the for loop where I noted the comment below has an issue, but for the life of me I cannot wrap my head around it and why it's not working. I understand basic for loops, but the structure it's been put into in this scenario is throwing me off a bit. From the demonstration by the professor I wrote down that line of code wrong (way off) and I get the results in the screenshot in the link below.

The program is suppose to loop asking "Guess again" until the number is guessed along with the hints provide. Unfortunately, the professor will not be available for quite a bit and I would like to get that line corrected. Hence, why I am asking for the help of you wonderful c++ experts. Thank you so very much for your time and guidance!!


https://www.dropbox.com/s/rsam28iu9kkidx6/for_loop_numbergame.jpg?dl=0


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

#include <iostream>
#include <sstream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;

bool die(const string & msg);

void game();

int main(){
	
	srand(static_cast<unsigned>(time(0)));

	game();
  
void game(){

	int answer = rand() % 100 + 1;
	int guess;

	cout << "Guess the number: ";
	cin >> guess || die("Input failure");

	if (guess == answer) {
		cout << "Wow, first guess is correct!" << endl;
		return;

	}

	int off = abs(guess - answer);
	for (off = 0; off != answer; off++) // <-- **I believe issues is here**
		cout << "Guess again: ";
	cin >> guess || die("Input failure");
	if (guess == answer){
		cout << "That's correct, congrats" << endl;
		return;
	}
	int newOff = abs(guess - answer);
	if (newOff < off)
		cout << "That's a better guess than last time." << endl;
	else
		if (newOff == off)
			cout << "That guess is equally good than last time." << endl;
		else
			cout << "That guess is worse than last time." << endl;
	off = newOff;

}

bool die(const string & msg) {
	cout << "Fatal error: " << msg << endl;
	exit(EXIT_FAILURE);

} // end die 
Last edited on
I believe I can see your problem. The problem is with line 35 as you suspected, because your for loop has no opening and closing curly brackets { and } it means that the for loop only applies to the single line that comes after it. To fix this you would simply need to wrap the code you need to loop around curly brackets like you would do for a function or an if statement.
Last edited on
Thank you. Made that correction and a couple of other modifications and it works now. Final code:

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

#include <iostream>
#include <sstream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;

bool die(const string & msg);

void game();

int main(){
	
	srand(static_cast<unsigned>(time(0)));

	game();

void game(){

	int answer = rand() % 100 + 1;
	int guess;

	cout << "Guess the number: ";
	cin >> guess || die("Input failure");

	if (guess == answer) {
		cout << "Wow, first guess is correct!" << endl;
		return;

	}

	int off = abs(guess - answer);

	for (guess = 0; guess != answer; guess++) {
		cout << "Guess again: ";
		cin >> guess || die("Input failure");
		if (guess == answer){
			cout << "That's correct, congrats" << endl;
			return;
		}
		int newOff = abs(guess - answer);
		if (newOff < off)
			cout << "That's a better guess than last time." << endl;
		else
			if (newOff == off)
				cout << "That guess is equally good than last time." << endl;
			else
				cout << "That guess is worse than last time." << endl;
		off = newOff;

	}

}

bool die(const string & msg) {
	cout << "Fatal error: " << msg << endl;
	exit(EXIT_FAILURE);

} // end die
Last edited on
Topic archived. No new replies allowed.