Thread: Uninitialized local variable

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    5

    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.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    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.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    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'?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    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
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    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?

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    Okay thanks I got it working right just had to open this project in a new file and it is compiling right. Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. uninitialized local variable 'XXXXXX' used
    By Brownie in forum C Programming
    Replies: 8
    Last Post: 02-26-2009, 06:41 AM
  2. Should "static" variable always be local to a file?
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 06-22-2008, 11:51 PM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. Printing part of a local variable
    By Phan in forum C Programming
    Replies: 10
    Last Post: 09-26-2005, 05:51 PM
  5. return local variable
    By sangi in forum C Programming
    Replies: 17
    Last Post: 10-22-2004, 03:40 AM