uninitialized local variable 'guess' used how to fix it!

Sep 4, 2020 at 10:57pm
It says C4700 uninitialized local variable 'guess' used. i dont get it, how do i fix it please help!
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
#include <iostream>
using namespace std;

int main() {
	int secretNum = 6;
	int guess;
	int guessLimit = 3;
	int guessCount = 0;
	bool OutOfGuess = false;

	while (secretNum != guess && !OutOfGuess) {
		if (guessCount < guessLimit) {
			cout << "Enter a guess : ";
				cin >> guess;
			guessCount++;
		}
		else {
			OutOfGuess = true;
		}
	}
	if(OutOfGuess) {
		cout << "You Lose!";
}
	else {
		cout << "You Win!";
	}
	return 0;
}
Sep 4, 2020 at 11:00pm
On line 6. you declare a variable called guess.
On line 11, you compare a value against the value of guess.

But nowhere did you give guess a proper value before line 11. Your program is technically undefined behavior because you are using the value of a variable that is uninitialized.

Just give it an initial value that doesn't equal secretNum, like 0.
int guess = 0;
Last edited on Sep 4, 2020 at 11:01pm
Sep 4, 2020 at 11:06pm
Thanx alot! Fixed it :D
Topic archived. No new replies allowed.