### 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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124`` `````` //***************************************************************************** // 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 #include #include 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 :)

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122`` ``````//***************************************************************************** // 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 #include #include 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.