While loop not incrementing

I have a function that is supposed to iterate a payment amount until it equals another variable amount. I think I'm misunderstanding something about while loop mechanics. Can someone please tell me why its not working?

When I debug, the loop takes in the variable values correctly: interest, salary, and the variable values all show as local values. But the guessPay doesn't increase, and the inSalary doesn't increment up.

What am I doing wrong here.

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
// function to iterate max loan amount from one third of annual salary
double funcMaxPrice(double inSalary, double inRate)
{
	//local variables
	double targetPay = 0.00;   // payment suggestion third of income
	double guessPay = 100.00;  // computed payment
	double i = 0.00;	   // monthly interest rate
	double exp = 0.00;	   // exponent for POW function

	// prep calculations for amort forumula
	targetPay = funcThirdSalary(salary);
	i = (inRate * .01) / PAYS_PER_YEAR;
	exp = -(PAYS_PER_YEAR * TOTAL_PAYS);

	// iteration loop based on incremental loan amount 
	while (targetPay <= (guessPay - 10.00))
	{
		//amort formula
		guessPay = (inSalary * i)     // numerator
			 /		   
			  1 - pow(( 1 + i),exp);  // denominator

		inSalary += 500;  // increment loan amount
		cout << guessPay << endl;
	}
		 
	return inSalary;
}// end funcMaxPrice  


Thanks
Please let me know if I'm not following protocol here- This is a great resource for me..
Last edited on
In the future, please put your code in [code][/code] tags.

You're division to calculate guessPay is incorrect. Division has a higher precedence than subtraction, so you divide (inSalary * i) by 1 (accomplishing nothing), before subtracting the result of the pow() function. This might not be the source of your problem, but you probably want to fix it.
Try using

inSalary = (inSalary+500); // increment loan amount

instead of

inSalary += 500; // increment loan amount

Sometimes a redefinition helps instead of an incremental value. Should be the same result as what you were looking for.

I've found that when using increments and decrements, you have to have ++ or -- to make them work properly. When I want to increase through multiplication or addition, the most fool-proof way to do it is through the use of parentheses and redefinitions.
@ Zhuge- Got it. Fixed it- may well be part of the problem THX.
@ soldieringon- Also Thx, I tried it but no joy.

I'm still not incrementing up.

Is a while loop the tool for the job here? Should I be using a for() loop? I'm never sure when to use which.

Wayne
Thanks both of you for the help. On a whim, I closed Visual Studio 2010 and reopened it. I think I had some other file loaded that was causing problems. Its working now, with the repair Zhuge suggested.

Excellent!!
Topic archived. No new replies allowed.