broken for loop or equation

Hello, I'm having issues somewhere either in my for loop or my equation. The program I was asked to create for my assignment gave me the variables and "equations" and I do everything else. My only issue is that my "Monthly Payments" print out isn't updating with my "noMonths" variable. It just keeps computing with an int of 12. Additionally, the monPayment equation doesn't actually create the monthly payments but I think I get points docked if I change it. (loanAmt * monIntRate + loanAmt) / noMonths seems more appropriate to me though.

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
  // Variables requiring previous input
		int noMonths = 12;
		double loanAmt = price - downPayment - tradeIn;
		double annualIntPercent = annualIntRate * 100.0;
		double monIntRate = annualIntRate / 12.0;
		double monPayment = (loanAmt * monIntRate)/(1.0-pow(-noMonths,(1+monIntRate)));

		//Output
		cout.setf(ios::fixed, ios::floatfield); cout.precision(2);
		cout << "\n" << endl;
		cout << "\n" << endl;
		cout << "Honest Dave's Used Cars" << endl;
		cout << "Vehicle Price:  " << price << endl;
		cout << "Trade-in value: " << tradeIn << endl;
		cout << "Down payment:   " << downPayment << endl;
		cout << "              " << "----------" << endl;
		cout << "Loan amount:    " << loanAmt << endl;
		cout << " " << endl;
		cout << "Annual Interest Rate: " << annualIntPercent << "%" << endl;
		cout << " " << endl;
		cout << "Monthly payment options" << endl;

		int increment = 12;

		// Had to use equation here instead of variable. Would repeat same value for all monthly payments.
		for (noMonths = 12; noMonths <= 60; noMonths += increment) {
			cout << noMonths << " Months:      " << monPayment << endl;
		}

I keep getting output
1
2
3
4
5
6
Monthly payment options
12 Months:      -nan(ind)
24 Months:      -nan(ind)
36 Months:      -nan(ind)
48 Months:      -nan(ind)
60 Months:      -nan(ind)


Also the monPayment written algebraically would be (loanAmt * monIntRate)/(1.0 –(1+monIntRate) –noMonths ) . noMonths being a negative exponent.

Even if I change the monPayment to an equation like (loanAmt * monIntRate + loanAmt) / noMonths.

The biggest problem is that monPayment doesn't update even though it uses noMonths which does change within the for loop.
You can't take the log of a negative number (line 6). Reconsider your formula.

You also write the same non-answer (monPayment) repeatedly in the loop on lines 26-28.

Last edited on
Topic archived. No new replies allowed.