Warning: Variable ' ' is uninitialized when used here

So on the bright side I have my code working for what I was trying to accomplish. However I am getting these warnings for line 16 for the variable stretch, breakage, and maxLength. Then on line 49 for addedPounds.

I'm confused on what it wants me to do. Is it just asking me to assign the variables a value here?

Also as a beginner any syntactical, formatting, etc. critique of my code is always appreciated. Have to learn somehow!

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
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <iomanip>

using namespace std;
//Functions
void input_data(double &length, double &thickness);
double cable_tension(double &length, double thickness, double stretch, double breakage, double maxLength, int pounds);

int main()
{   //Variables
    double length, thickness, stretch, breakage, maxLength;
    int pounds = 0;

    //Calling functions
    input_data(length, thickness);
    cable_tension(length, thickness, pounds, stretch, breakage, maxLength);

    return 0;
}

//Input, calcualtion, and output functions
void input_data(double &length, double &thickness)
{
    cout << "Enter the lenght of the cable in feet: ";
    cin >> length;
    cout << "Enter the thickness of the cable up to 2.0 inches: ";
    cin >> thickness;

    while (length <= 0)
    {
        cout << "\nThis is not a valid cable length." << endl;
        cout << "Enter a cable length greater than 0: " << endl;
        cin >> length;
    }
    while (thickness <= 0 || thickness > 2.0)
    {
        cout << "\nThe cable thickness must be 2.0 inches or less." << endl;
        cout << "Enter a cable thickness less than 2.0: ";
        cin >> thickness;
    }
}
//function to do the stretching / breakage calc
double cable_tension(double &length, double thickness, double stretch, double breakage, double maxLength, int pounds)
{
    pounds;
    stretch = thickness * .3;
    breakage = length * 16 / 100;
    maxLength = breakage + length;
    double addedPounds;

    while (length + addedPounds <= maxLength)
    {
        addedPounds = pounds * stretch;

        cout << fixed << setprecision(2);
        cout << "\nThe tension applied to the cable is: " << pounds << " lbs" << endl;
        cout << "The length of the cable is: " << length + addedPounds << " feet" << endl << endl;

        pounds++;

    }
    cout << "The cable broke!" << endl;
    return pounds;
}
Last edited on
pounds, stretch, breakage, and maxLength should be defined in cable_tension, not in main. If you think about it, it doesn't make any sense to pass in some random (uninitialized) value in these parameters and then use the parameters as local variables.

If it still complains about length and thickness, just initialize them both to 0.

And line 45 above is a nop.
Last edited on
You have:
1
2
3
4
5
    breakage = length * 16 / 100;
    maxLength = breakage + length;
    double addedPounds;

    while (length + addedPounds <= maxLength)

Lets pretend that someone did set length=0. What do we get?
1
2
3
4
5
    breakage = length * 16 / 100; // 0 * 16 / 100 == 0
    maxLength = breakage + length; // 0 + 0 == 0
    double addedPounds;

    while (length + addedPounds <= maxLength) // 0 + addedPounds <= 0 

Or bit differently:
1
2
double addedPounds;
bool fubar = ( addedPounds <= 0 );

Is the fubar true or false? Explain why.

The compiler says that 'addedPounds' is uninitialized, value not set, undefined. The compiler thus says that it does not know for sure what the value of fubar will be.

We can help the compiler. We can state exact facts:
1
2
double addedPounds = 3.14;
bool fubar = ( addedPounds <= 0 );

Now all know for certain that fubar is false.

So on the bright side I have my code working for what I was trying to accomplish.

No. On the dark side the result that you do get from the program unfortunately seems what you expect. It would have been better if it had produced less believable nonsense. For someone else it probably does.

At least the compiler does make remarks and you did pay attention to them. That is the bright side; there is hope for you.


Another note. You have:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

double cable_tension( int pounds )
{
    std::cout << pounds;
    return pounds;
}

int main()
{
    double maxLength;
    /* nothing */ cable_tension( maxLength );
}

Yes, your function takes an integer argument, but you pass an undefined double value to it.
Finally, the function returns a integer value as a double, but the caller does not use the returned value in any way.
Topic archived. No new replies allowed.