Variable "--" being used without being initialized

Hey, I am taking a introduction to Computer Science and I am having difficult in finding a solution. So I keep getting the error "Variable integer being used without being initialized" the error occurs at "if (integer.... etc)" apparently. I think the error may be due to the fact im using two "if else" statements but im not sure how to combine both of them.

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
  #include <iostream>	//For cout and cin
#include <conio.h>	//for getch
#include <iomanip>	//display output more neatly/organized

using namespace std;

int main()
{

	//variables
	int lowValue;	//low value range
	int highValue;	//high value range
	int integer;	//integer entered for range check

	//main title
	cout << "Integer Range Checker" << endl;
	cout << "-----------------------" << endl;
	cout << endl;

	//Ask for high and low values and integer
	cout << "Please enter in a integer for the Low Value: " << endl;
	cin >> lowValue;
	cout << endl;
	cout << "Please enter in a integer for the High Value: " << endl;
	cin >> highValue;
	cout << endl;
	
	//Validation for Low and High Value
	if (highValue <= lowValue)
	{
		cout << "Error as occured; the Low Value is greater than High Value, please try again." << endl;
	}
	else
	{
		cout << "Please enter a integer: " << endl;
		cin >> integer;
		cout << endl;
	}

	//Validation for Integer ERROR OCCURS BELOW
	if (integer >= lowValue && integer <= highValue)
	{
		cout << " The Integer " << integer << " is within the range." << endl;
	}
	else
	{
		cout << "Error as occured; the integer entered is not within the range, please try again" << endl;
	}

	_getch();	//hold the screen
}
if (highValue <= lowValue) -> true
the variable integer is uninitialized. Solution: move the if on line 41 inside the else on line 33
I have run the code and all is ok. I guess it's the matter of a compiler too old.
Please tell us which is "Variable integer being used without being initialized" as said the compiler?
Last edited on
closed account (o3hC5Di1)
Hi there,

There are two different terms regarding the "state" of a variable:

- Declaration: int i;. This is you telling the compiler "I'll be using a variable called "i" and it will hold an integer, so you will need to reserve 4 bytes of memory for it."

- Initialization: i = 1000; //or std::cin >> i . This is you telling the compiler "Remember that variable "i" I told you about? I would like to store the number 1000 into it."

If you only declare a variable, but do not initialize it, it may hold some garbage value that was already it memory at the exact same place where now "i" is stored. This will lead to unexpected behaviour, which is why the compiler is warning you about it.

Just for the record, initialization and declaration can be done in a single line sometimes:

int i = 1000;

Hope that helps.

All the best,
NwN
code777, what do you mean by moving it inside line 33 i tried and it seems to just give me an error
condor it was line 41 that is the integer that is giving me errors
the compiler is warning

That is a key point. Compilers can both warn of potential dangers and tell that something is a clear error. Compiler's options determine how verbose it is.

@condor: It is not the age of compiler, but its verbosity features. Actually, older compilers were much less helpful. If your compiler does not point out this, how many other issues does it silently accept?
closed account (o3hC5Di1)
Hi there,

Mr. Coder777 advised you to move the code because of this:

1
2
3
4
5
6
7
8
9
10
else
	{
		cout << "Please enter a integer: " << endl;
//This is the only place you ever give a value to "integer" is this else-block is not called, integer will never have a value
		cin >> integer;  
		cout << endl;
	}
//So when you get here, and the else-block wasn't called, integer will have a garbage value resulting in unexpected behaviour
	//Validation for Integer ERROR OCCURS BELOW
	if (integer >= lowValue && integer <= highValue)


So moving that if/else block after cin >> integer; will not give you the same warning:

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//Validation for Low and High Value
	if (highValue <= lowValue)
	{
		cout << "Error as occured; the Low Value is greater than High Value, please try again." << endl;
	}
	else
	{
		cout << "Please enter a integer: " << endl;
		cin >> integer;
		cout << endl;

                //Validation for Integer ERROR OCCURS BELOW
		if (integer >= lowValue && integer <= highValue)
		{
			cout << " The Integer " << integer << " is within the range." << endl;
		}
		else
		{
			cout << "Error as occured; the integer entered is not within the range, please try again" << endl;
		}
	}



Hope that helps.

All the best,
NwN
There is also a problem if you do initialize but don't move the if on line 41. The value you initialized the variable with may be in the low-high range and you would get a false positive.
@keskiverto

I saw in the canucksfan1's message: error "Variable integer being used without being initialized" but not warning...I use my compiler(gcc (GCC) 4.8.1 Copyright (C) 2013 Free Software Foundation, Inc.)with the following command line:

g++ -Wall -o "foo" "foo.cpp"

and is very verbose :) So this means - of course among other warnings - the compiler warns me about uninitialized variables. As for the runtime errors...this is another story...
Last edited on
Look at some outputs for some choices of limits. (The input dialog was simplified for a more compact output).

Compilation: g++ -Wall -o "foo" "foo.cpp"

(with no warning/errors)

and some outputs:

[condor@archie ~]$ ./foo
Integer Range Checker
-----------------------
Please enter in a integer for the Low Value: 5
Please enter in a integer for the High Value: 20
Please enter a integer: 3
Error as occured; the integer entered is not within the range, please try again
[condor@archie ~]$ ./foo
Integer Range Checker
-----------------------
Please enter in a integer for the Low Value: -5
Please enter in a integer for the High Value: 6
Please enter a integer: 0
The Integer 0 is within the range.
[condor@archie ~]$ ./foo
Integer Range Checker
-----------------------
Please enter in a integer for the Low Value: -20
Please enter in a integer for the High Value: -5
Please enter a integer: 2
Error as occured; the integer entered is not within the range, please try again
[condor@archie ~]$ ./foo
Integer Range Checker
-----------------------
Please enter in a integer for the Low Value: 5
Please enter in a integer for the High Value: 10
Please enter a integer: 15
Error as occured; the integer entered is not within the range, please try again

(My machine works on Linux platform.)
Thank you so much NwN i spent a few hours trying to figure out what the program was and it was just so simple! I feel so silly :P. else-block you mean the else portion of the "if statement"?

So the reason for the error was I made two separate "if/else statements" meaning I ended the first one with a }, which was wrong because if the first else-block was not called then the second "if statement" could not perform because no integer value was given?

Would you then say always writing "if statements" within each other for example I ended the first "if statement with } and started the new if statement, but what you told me to do is put the second "if statement" after the first without closing it until the very end.


Thank you
Topic archived. No new replies allowed.