uninitialized local variable error even thought I initialized it (C++)

2425 views c++
8

I'm pretty new to programming and not sure what I did wrong but I'm getting the error on line 20 it says that I haven't initialized intownMiles and highwayMiles

include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    double intownMPG = 23.5;
    double highwayMPG = 28.9;
    double intownMiles;
    double highwayMiles;
    double gallons = intownMiles / intownMPG + highwayMiles / highwayMPG;

    cout << "Please enter the number of in-town driving miles:  " << endl;
    cin >> intownMiles;
    cout << "Please enter the number of highway driving miles:  " << endl;
    cin >> highwayMiles;
    cout << "The total number of gallons required is: " << gallons  << "gal" << endl;

}

answered question

Well, you indeed haven't initialized intownMiles and highwayMiles... You have only declared them without giving any initial values (and compiler won't do it for you). Note that cin >> intownMiles; will be executed after the value of gallons is calculated.

I think you may have a very basic misunderstanding of how a C++ program works - the statements are executed sequentially.

"using namespace std;" - bad habit. It will bite you when your code gets large. Just don't.

2 Answers

12

You've just declared the variable.

There are two things you've to do with variable before using them:

  • Declaration: Declare the variable, i.e., Declare its data-type and name. Example: int classNumber;

  • Initialization: Give your declared variable an initial value. Example: classNumber = 10;

And you can do Dynamic Initialization of your variable, that means, Give your variable an initial value right after the declaration. Example:

int classNumber = 10;

And, Compiler will execute your line by line, so the point is When your compiler will reach this location

double gallons = intownMiles / intownMPG + highwayMiles / highwayMPG;

Then it will raise an error because right now two variables, intownMiles and highwayMiles are not initialized, are only declared.

So transport that line at the end of the current code, like so:

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    double intownMPG = 23.5;
    double highwayMPG = 28.9;
    double intownMiles;
    double highwayMiles;

    cout << "Please enter the number of in-town driving miles:  " << endl;
    cin >> intownMiles;
    cout << "Please enter the number of highway driving miles:  " << endl;
    cin >> highwayMiles;
    cout << "The total number of gallons required is: " << gallons  << "gal" << endl;
    double gallons = intownMiles / intownMPG + highwayMiles / highwayMPG;
}

posted this
0

Your code:

double intownMiles;
double highwayMiles;
double gallons = intownMiles / intownMPG + highwayMiles / highwayMPG;

You clearly have not initialized those variables before using them. All you did was declare them - so they exist, but have indeterminate values until you assign to them (which you never do).

Initialize the variables to sane initial values when you declare them and the compiler warning will go away (and your code will no longer have Undefined Behaviour).

posted this

Have an answer?

JD

Please login first before posting an answer.