uninitialized local variable

I'm not sure this will post in a readable format, so I apologize in advance for that. (I read how to input it, but my computer is not showing the (#) edit symbol anywhere.

I am getting a lot of warnings and several "uninitialized local variable" errors for every one of my input variables. I've not had this issue before, I'm assuming it's because I've done the looping incorrectly.

This is only for Chapters 1-6, so we are NOT allowed to use anything more complex than the basics, up through the use of While loops. He is specifically asking for While loops.

This is what I get when I run the program:

(29,29): warning C4244: '=': conversion from 'double' to 'float', possible loss of data

(30,35): warning C4244: '=': conversion from 'double' to 'float', possible loss of data

(32,41): warning C4244: '=': conversion from 'double' to 'float', possible loss of data

(44,10): warning C4804: '>': unsafe use of type 'bool' in operation

(29): error C4700: uninitialized local variable 'annualIntRate' used

(31): error C4700: uninitialized local variable 'price' used

(31): error C4700: uninitialized local variable 'downPayent' used

(31): error C4700: uninitialized local variable 'tradeIn' used

(32): error C4700: uninitialized local variable 'noMonths' used

I am asking for specific inputs and have identified them in the beginning, I've searched the forums and the web and have been unable to figure out what I did wrong.


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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
 //*******************************************************************************************
//
// Midterm
// This program is designed to calculate the monthly car payments for a customer
//
//*******************************************************************************************

#include <iostream>
#include <iomanip>							// for setwidth
#include <cmath>

using namespace std;
int main()

// Input Variables and formulas
{
	float price;					// price of vehicle
	float downPayent;				// down payment
	float tradeIn;					// trade in value
	float loanAmount;				// loan amount (calculated)
	float annualIntPercent;			// annual interest percent (calculated)
	float annualIntRate;			// annual interest rate (calculated)
	int noMonths;					// number of monthly payments (12, 24, 36, 48, 60)
	float monPayment;				// Monthly payment (calculated)
	float monIntRate;				// monthly interest rate (calculated)
	int i=1;						// count initializer for needed information. 
	int e = 6;						// will be used to end and restart program

	monIntRate = annualIntRate / 12.0;
	annualIntPercent = annualIntRate * 100.0;
	loanAmount = price - downPayent - tradeIn;
	monPayment = (loanAmount * monIntRate) / (pow(1.0 - (1 + monIntRate), -noMonths));


	// Get Price loop
	cout << "Please enter price of vehicle" << endl;
	cin >> price;
	while (i==1)
	{
		while (price > 50.00 || price < 50000.00)
		cin >> price;
		i++;
	
		while (!price > 50.00 || price < 50000.00)
		cout << "Incorrect price entered. Please make sure you use decimal format, with no comma.<<endl; (example: 25,000 would be entered as 25000.00)<<endl;<<endl;<< Price must be greater than $50.00 and less than $50,000.00<<endl;<<endl; Please enter the number 6 to restart" << endl;
		cin >> e;
		while (e==6)
			return 0;
	}
	while (i == 2)
	{
		cout << "Please enter trade in value" << endl;
		cin >> tradeIn;
		{
			while (tradeIn >= 0 && tradeIn < price)
				cin >> tradeIn;
			i++;
		}
		
	}
	while (i == 3)
	{
		cout << "Please enter down payment" << endl;
		cin >> downPayent;
		{
			while (downPayent >= 0 && downPayent < (price - tradeIn))
				cin >> downPayent;
			i++;
		}

	}
	while (i == 4)
	{
		cout << "Please enter annual interest rate.<<endl;<<Interest rate must be entered in fraction format<<endl;<<(example: 6% would be entered as .06)<<endl;<<endl;<< Annual interest rate must be greater than, or equal to, 0, and be less than .50" << endl;
		cin >> annualIntRate;
		{
			while (annualIntRate >= 0 && annualIntRate < .50)
				cin >> annualIntRate;
			i++;
		}
	}
	while (i == 5)
	{
		cout << "You're monthly payment for a 12 month loan is: " << monPayment << 12;
		cout << "You're monthly payment for a 24 month loan is: " << monPayment << 24;
		cout << "You're monthly payment for a 36 month loan is: " << monPayment << 36;
		cout << "you're monthly payment for a 48 month loan is: " << monPayment << 48;
		cout << "you're monthly payment for a 60 month loan is: " << monPayment << 60;
		i++;
	}
	while (i == 6)
	{
		cout << "Vehicle Price: " << price;
		cout << "Trade in Value: " << tradeIn;
		cout << "Down Payment: " << downPayent;
		cout << "------------------------------------";
		cout << "Loan Amount: " << loanAmount << endl;
		cout << endl;
		cout << "Annual Interest Rate: " << annualIntPercent;
		i++;
	}
	while (i == 7)
	{
		return 0;
	}
}
Last edited on
For a start:
1. Your program runs without errors
2. You should use double throughout instead of float
3. I doubt whether your teacher prohibits a switch
4. A while is useful too but should be adopted/chosen/designed appropriately
Here's one way. There's a pattern to the input parts - probably need a bit of explanation added inside while loops.


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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//*******************************************************************************************
//
// Midterm
// This program is designed to calculate the monthly car payments for a customer
//
//*******************************************************************************************

#include <iostream>
#include <iomanip>                            // for setwidth
#include <cmath>

using namespace std;
int main()

// Input Variables and formulas
{
    double price{};                    // price of vehicle
    double downPayent{};                // down payment
    double tradeIn{};                    // trade in value
    double loanAmount{};                // loan amount (calculated)
    double annualIntPercent{};            // annual interest percent (calculated)
    double annualIntRate{};            // annual interest rate (calculated)
    int noMonths{};                    // number of monthly payments (12, 24, 36, 48, 60)
    double monPayment{};                // Monthly payment (calculated)
    double monIntRate{};                // monthly interest rate (calculated)
    
    //     Get Price loop
    while
        (
         cout << "Please enter price of vehicle: " &&
         cin >> price &&
         ( (price < 50.0) || (price > 5000.) )
         )
    {
        cout << "Price out of range. Do it again!\n";
    }
    
    //     Get Trade-In value loop
    while
        (
         cout << "Please enter trade in value: " &&
         cin >> tradeIn &&
         ( (tradeIn < 0) || (tradeIn > price) )
         )
    {
        cout << "Trade in out of range. Do it again!\n";
    }
    
    
    //     Get Down payment loop
    while
        (
         cout << "Please enter down payment: " &&
         cin >> downPayent &&
         ( (downPayent < 0) || (downPayent > (price - tradeIn) ) )
         )
    {
        cout << "Down payment out of range. Do it again!\n";
    }
    
    //     Get Interest rate loop
    while
        (
         cout << "Please enter interest rate (eg 3% = .03): " &&
         cin >> annualIntRate &&
         ( (annualIntRate < 0) || (annualIntRate > .05) )
         )
    {
        cout << "Interest rate out of range. Do it again!\n";
    }
    
    monIntRate = annualIntRate / 12.0;
    annualIntPercent = annualIntRate * 100.0;
    loanAmount = price - downPayent - tradeIn;
    monPayment = (loanAmount * monIntRate) / (pow(1.0 - (1 + monIntRate), -noMonths));
    
    cout
    << '\n'
    << "       Vehicle Price: $" << price << '\n'
    << "      Trade in Value: $" << tradeIn << '\n'
    << "        Down Payment: $" << downPayent << '\n'
    << "------------------------------------\n"
    << "         Loan Amount: $" << loanAmount << '\n'
    << "Annual Interest Rate:  " << annualIntPercent << "%\n";
    
    
    double factor{};
    int i{};
    int no_Months{};
    
    while (i < 5)
    {
        no_Months = (i+1)*12;
        
        factor = pow(1 + annualIntRate/12., no_Months);
        monPayment = loanAmount * monIntRate*factor/(factor - 1);
        
        cout
        << "Your monthly payment for a " << no_Months << " month loan is: "
        << monPayment
        << " (Total of all payments: $" << monPayment * no_Months << ")\n";
        
        i++;
    }
    
    return 0;
}

Please enter price of vehicle: 3456
Please enter trade in value: 123
Please enter down payment: 456
Please enter interest rate (eg 3% = .03): .034

       Vehicle Price: $3456
      Trade in Value: $123
        Down Payment: $456
------------------------------------
         Loan Amount: $2877
Annual Interest Rate:  3.4%
Your monthly payment for a 12 month loan is: 244.188 (Total of all payments: $2930.26)
Your monthly payment for a 24 month loan is: 124.167 (Total of all payments: $2980)
Your monthly payment for a 36 month loan is: 84.1748 (Total of all payments: $3030.29)
Your monthly payment for a 48 month loan is: 64.1903 (Total of all payments: $3081.14)
Your monthly payment for a 60 month loan is: 52.2089 (Total of all payments: $3132.53)
Program ended with exit code: 0
Last edited on
Lets look at this part:
1
2
3
4
	float annualIntRate;			// annual interest rate (calculated)
	float monIntRate;				// monthly interest rate (calculated)

	monIntRate = annualIntRate / 12.0;

Line 1: the initial value of annualIntRate is not set. It is uninitialized.
Line 2: the initial value of monIntRate is not set. It is uninitialized.

Line 3: You calculate annualIntRate / 12.0. The value of annualIntRate is unknown.
How much is unknown / 12.0? An unknown value.
You use the value of annualIntRate before you set it. That is a logical error.

Second, the annualIntRate is float and the 12.0 is double.
The first step is thus to implicitly convert the float into double and do "double divides double". The (unknown) result value has type double.

Then there is monIntRate = result_of_division;. The result_of_division is double.
That value does not fit into monIntRate, which is float. Hence the other warning.

You do set the value monIntRate before you use it, which is ok.
However, the value you set is unknown and hence the value of monIntRate remains unknown.

You need to set the values before you use them. The {} in againtry's code initializes variable with known value:
1
2
3
4
	float annualIntRate {}; // same as float annualIntRate = 0.0f;
	float monIntRate {}; // same as float monIntRate = 0.0f;

	monIntRate = annualIntRate / 12.0;

Now we know exactly what happens on line 4: monIntRate = 0.0f / 12.0; // same as: monIntRate = 0.0;

Alas, that truncation from double to float. You could use double, as againtry does, or you could use float:
1
2
3
4
	float annualIntRate {};
	float monIntRate {};

	monIntRate = annualIntRate / 12.0f;

The 12.0f has type float. Therefore, the division is float/float and the result value has type float too.
Last edited on
Topic archived. No new replies allowed.