# Uninitialized local variable

• 12-03-2010
steeno321
Uninitialized local variable
I am having trouble figuring out why I am getting an Unitialized Local Variable error in my code. The program calculates the charge associated with water depending on usage.
Here is the code:
Code:

```// Description: Calculate the water bill given the cubic feet of water used for Eureka Water Company, which charges the homeowner based on the following rules. /* a. A flat rate of \$15.00 for usage up to and including 1000 cubic feet. b. \$0.0175 per cubic foot for usage over 1000 cubic feet and up to and including 2000 cubic feet. c. \$0.02 per cubic foot for usage over 2000 cubic feet and up to and including 3000 cubic feet. d. A flat rate of \$70.00 for usage over 3000 cubic feet. */ #include <iostream> using namespace std; void Read(int usage, double Charge); void Print(double Charge); void main () {         int usage;         double Charge;         int x = 0;         x = (x + 1); Read(usage); if(usage <= 1000)         Charge = 15.00; else if (usage > 1000 && usage <= 2000)         Charge = 15.00 + (0.0175 * x); else if (usage > 2000 && usage <=3000)         Charge = 15.00 + 17.50 + ( 0.02 * x); else         Charge = 70.00; Print(Charge); } void Read(int usage, double Charge) {         // 1. Start         int usage = 0;         // 2. Prompt for Input         cout << "Enter Water Usage: ";         // 3. Read Input         cin >> usage;         // 4. Exit (Input returns via ref-param) } void Print(double Charge) {         // 1. Start         // 2. Print Output         cout << "The charge is :" << Charge << endl;         // 3. Exit }```
The error shows
Code:

```2>c:\users\tyler\documents\cs104labtemplate.cpp(31): warning C4700: uninitialized local variable 'usage' used 2>  lab6.vcxproj -> C:\Users\Tyler\Documents\Visual Studio 2010\Projects\lab6\Debug\lab6.exe 1>  lab5.2.vcxproj -> C:\Users\Tyler\Documents\Visual Studio 2010\Projects\lab6\Debug\lab5.2.exe ========== Rebuild All: 2 succeeded, 0 failed, 0 skipped ==========```
I dug around on the forums and other C++ related sites and didn't find anything that would help fix the problem. Any help would be appreciated. Thanks.
• 12-03-2010
GReaper
In the 'read' function you define another integer with exactly the same name!

Edit: Your 'read' function will not work the way you think it will... Pass a reference or a pointer instead of a value.
• 12-03-2010
steeno321
Sorry I am just beginning to learn and use C++. I was under the assumption the 'Read' function assigned the usage value into the 'Main'? Do I need to reference the usage from 'Read' and then implement it into the 'Main' function? Or should I rename the usage in 'Read' to something else and assign a value to the usage in 'Main'?
• 12-03-2010
GReaper
By default, C++ makes passes by value to a function. To make it pass a reference do this:
Code:

`void Read(int &usage, double Charge)`
Edit: And remove the 'int' in your 'Read' function
• 12-03-2010
steeno321
I tried adding a reference to the usage and removing int from 'Read'. I still am getting the same error. The changes are:
Code:

```// Description: Calculate the water bill given the cubic feet of water used for Eureka Water Company, which charges the homeowner based on the following rules. /* a. A flat rate of \$15.00 for usage up to and including 1000 cubic feet. b. \$0.0175 per cubic foot for usage over 1000 cubic feet and up to and including 2000 cubic feet. c. \$0.02 per cubic foot for usage over 2000 cubic feet and up to and including 3000 cubic feet. d. A flat rate of \$70.00 for usage over 3000 cubic feet. */ #include <iostream> using namespace std; void Read(int &usage, double Charge); void Print(double Charge); void main () {         int usage;         double Charge;         int x = 0;         x = (x + 1); Read(usage); if(usage <= 1000)         Charge = 15.00; else if (usage > 1000 && usage <= 2000)         Charge = 15.00 + (0.0175 * x); else if (usage > 2000 && usage <=3000)         Charge = 15.00 + 17.50 + ( 0.02 * x); else         Charge = 70.00; Print(Charge); } void Read(int &usage, double Charge) {         usage = 0;         cout << "Enter Water Usage: ";         cin >> usage;        } void Print(double Charge) {         // 1. Start         // 2. Print Output         cout << "The charge is :" << Charge << endl;         // 3. Exit }```
What am I missing?
• 12-03-2010
iMalc
No, with that code you just posted your uninitialised variable warning will be gone.
You will however be getting an error relating to passing only one parameter to Read, where the Read function is defined as taking two parameters.

If you think about it, as it is there isn't any reason that the Read function should be declared as taking that second parameter.
• 12-03-2010
steeno321
Okay thanks I got it working right just had to open this project in a new file and it is compiling right. Thanks again!