Help with int variables

So I'm making a program that takes a price a trade-in value and a down payment with an interest rate to find a loan amount and then break that loan amount into 5 different length loans. The only thing I can't figure out how to do is get it to display the 12 month loan because if I set int noMonth = 0 I don't get a table at all and if I set it to 12 I don't get the 12 month to display

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  //*****************************************************************************
// This program will calculate monthly car payments for customers.
// Considering the price of the vehicle, any trade in value, and down payments.
// to formulate a loan amount using an annual interest rate and outputting
// the results for five different lengths of loans in a table.
//*****************************************************************************
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

// While loop to return the price of the vehicle.
float getPrice()
{
	float price;
	{
		cout << "Please enter vehicle price: ";
		cin >> price;
		if (price > 50000.00 || price < 50.0)
		cout << "Please enter a price between 50 and 50000 dollars." << endl;
	}
	while (price > 50000.00 || price < 50.0);
	return price;
}

// While loop to return the trade in value of the vehicle.
float getTradeIn(float price)
{
	float tradeIn;
	{
		cout << "Please enter trade in value: ";
		cin >> tradeIn;
		if (tradeIn < 0 || tradeIn >= price)
		{
		cout << "Please enter a value greater than zero and less than the price of the car." << endl;
		}
	}
	while (tradeIn < 0 || tradeIn >= price);
	return tradeIn;
}

// While loop to return the down payment.
float getDownPayment(float price, float tradeIn)
{
	float downPayment;
	{
		cout << "Please enter the amount of the down payment: ";
		cin >> downPayment;
		if (downPayment < 0 || downPayment >(price - tradeIn))
		{
			cout << "Please enter a value greater than zero and less than the price minus trade in." << endl;
		}
	}
	while (downPayment < 0 || downPayment >(price - tradeIn));
	return downPayment;
}

// While loop to return the interest rate.
double getInterestRate()
{
	double annualIntRate;
	{
		cout << "Please enter the interest rate. (Ex. 6% should be entered as .06):";
		cin >> annualIntRate;
		if (annualIntRate < 0 || annualIntRate >= 0.50)
			cout << "Please enter an interest rate greater than zero and less than .50";
	}
	while (annualIntRate < 0 || annualIntRate >= 0.50);
	return annualIntRate;
}

// Calculates the payments for the instructed months.
float calcMonPayament(float loanAmt, float monIntRate, int noMonths)
{
	float pay = (loanAmt * monIntRate) / (1.0 - pow((1 + monIntRate), -noMonths));
	return pay;
}

// Main portion of program.
int main()
{
	// Declared variables.
	float price;
	float downPayment;
	float tradeIn;
	float loanAmt;
	float annualIntRate;
	float annualIntPercent;
	float monIntRate;
	int noMonths;
	float monPayment;
	price = getPrice();
	tradeIn = getTradeIn(price);
	downPayment = getDownPayment(price, tradeIn);
	loanAmt = price - downPayment - tradeIn;
	annualIntRate = getInterestRate();
	annualIntPercent = annualIntRate * 100.0;
	monIntRate = annualIntRate / 12.0;
	noMonths = 12;
	
	cout << "   Honest Dave's Used Cars" << endl << endl;
	cout << "   Vehicle price:          $" << fixed << setprecision(2) << price << endl;
	cout << "   Trade-In Value:         $" << fixed << setprecision(2) << tradeIn << endl;
	cout << "   Down Payment:           $" << fixed << setprecision(2) << downPayment << endl;
	cout << "                -----------------" << endl;
	cout << "   Loan Amount:            $" << fixed << setprecision(2) << loanAmt << endl << endl;

	cout << "   Annual Interest Rate:   " << annualIntPercent << "%" << endl << endl;

	cout << "   Monthly Payment Options:" << endl;
	cout << "   ";

	while (noMonths)
	{
		if (noMonths < 60)
		{
			noMonths = (noMonths + 12);         // To add the 12 months between options
			monPayment = (loanAmt * monIntRate) / (1.0 - pow(1 + monIntRate, -noMonths));
			cout << noMonths << " Months              $" << fixed << setprecision(2) << monPayment << endl << "   ";
		}
	}

	return (0);
}
Last edited on
Hi

Your while statements do nothing, did you mean do while ? Put the keyword do before line 16, same for the other functions. Place the while part on the same line as the closing brace, makes it obvious that it is a do loop.

Tell us about the logic of line 113.

Prefer to use double rather than float, the precision of float is easily exceeded

Line 118 could make use of the calcMonPayament function.

Line 99 sets
noMonths
, but line 117 increases it, that's confusing.

Line 117 could be written:

noMonths += 12;
@OP
You have many problems with your code. This gets you on the right track. How accurate and meaningful it is, is up to you. You'll need to carefully examine what changes I have made - again up to you :)

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//*****************************************************************************
// This program will calculate monthly car payments for customers.
// Considering the price of the vehicle, any trade in value, and down payments.
// to formulate a loan amount using an annual interest rate and outputting
// the results for five different lengths of loans in a table.
//*****************************************************************************
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

// While loop to return the price of the vehicle.
float getPrice()
{
    float price{0};
    
    do{
        cout << "Please enter vehicle price: ";
        cin >> price;
    }while (price > 50000.00 || price < 50.0);
    
    return price;
}

// While loop to return the trade in value of the vehicle.
float getTradeIn(float price)
{
    float tradeIn{0};
    
    do{
        cout << "Please enter trade in value: ";
        cin >> tradeIn;
    }while ( tradeIn < 0 || tradeIn >= price );
    return tradeIn;
}

// While loop to return the down payment.
float getDownPayment(float price, float tradeIn)
{
    float downPayment{0};
    
    do{
        cout << "Please enter the amount of the down payment: ";
        cin >> downPayment;
    }while ( downPayment <= 0 || downPayment > (price - tradeIn) );
    
    return downPayment;
}

// While loop to return the interest rate.
double getInterestRate()
{
    double annualIntRate{0};
    
    do{
        cout << "Please enter the interest rate.: ";
        cin >> annualIntRate;
    } while ( annualIntRate <= 0. || annualIntRate > 0.05 );
    
    return annualIntRate;
}

// Calculates the payments for the instructed months.
float calcMonPayament(float loanAmt, float monIntRate, int noMonths)
{
    float pay = (loanAmt * monIntRate) / (1.0 - pow((1 + monIntRate), -noMonths));
    return pay;
}

// Main portion of program.
int main()
{
    // Declared variables.
    float price;
    float downPayment;
    float tradeIn;
    float loanAmt;
    float annualIntRate;
    float annualIntPercent;
    float monIntRate;
    int noMonths;
    float monPayment;
    
    price = getPrice();
    tradeIn = getTradeIn(price);
    downPayment = getDownPayment(price, tradeIn);
    
    loanAmt = price - downPayment - tradeIn;
    
    annualIntRate = getInterestRate();
    
    annualIntPercent = annualIntRate * 100.0;
    monIntRate = annualIntRate / 12.0;
    noMonths = 12;
    
    cout << "   Honest Dave's Used Cars" << endl << endl;
    cout << "   Vehicle price:          $" << fixed << setprecision(2) << price << endl;
    cout << "   Trade-In Value:         $" << fixed << setprecision(2) << tradeIn << endl;
    cout << "   Down Payment:           $" << fixed << setprecision(2) << downPayment << endl;
    cout << "                -----------------" << endl;
    cout << "   Loan Amount:            $" << fixed << setprecision(2) << loanAmt << endl << endl;
    
    cout << "   Annual Interest Rate:   " << annualIntPercent << "%" << endl << endl;
    
    cout << "   Monthly Payment Options:" << endl;
    cout << "   ";
    
    while (noMonths <= 60)
    {
        
        monPayment = (loanAmt * monIntRate) / (1.0 - pow(1 + monIntRate, -noMonths));
        cout
        << noMonths << " Months              $"
        << fixed << setprecision(2) << monPayment << endl << "   ";
        noMonths = (noMonths + 12);
    }
    
    std::cout << "End\n";
    
    return (0);
}


Please enter vehicle price: 10000
Please enter trade in value: 900
Please enter the amount of the down payment: 90
Please enter the interest rate.: .04
   Honest Dave's Used Cars

   Vehicle price:          $10000.00
   Trade-In Value:         $900.00
   Down Payment:           $90.00
                -----------------
   Loan Amount:            $9010.00

   Annual Interest Rate:   4.00%

   Monthly Payment Options:
   12 Months              $767.20
   24 Months              $391.26
   36 Months              $266.01
   48 Months              $203.44
   60 Months              $165.93
   End
Program ended with exit code: 0

Topic archived. No new replies allowed.