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
Your while statements do nothing, did you mean dowhile ? 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.
@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 :)
//*****************************************************************************
// 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>
usingnamespace 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