I am trying to make the exit condition for a do ... while loop become true by assigning variables within the loop. As of right now, im stuck in an infinite.
Not sure if this is even possible. Any help is most appreciated!
Thanks.
monthlyPayment gets smaller and smaller every loop, as does loanAmount, so I can only assume that monthlyPayment gets far too small and it takes too long for loanAmount to get below zero.
Also, you could replace the first two lines in the loop with
loanAmount -= monthlyPayment
maybe show us your main() where you call calculate() if you still can't sort it out
This is the kind of problem that debuggers were made for. Set a conditional breakpoint. It is a situation where loanamount will get really really small but never to 0 or below. Ask yourself this. How would the loanAmount ever reach zero?
Ah yes, I was calculating monthly payment incorrectly. I think it is correct now and I am getting mixed results.
The program will run correctly dependent upon what I put in. For example:
If I put in 100 (loan amount) 7.5 (interest rate) number greater than or equal to 8 (monthly payment)
It works and loops fine.
For each number more I add to the monthly payment the loop execution count decreases. (so, for 8 it looped 24 times, for 9 16 times, for 10, 13 times, etc.)
If i enter 7 or less for the monthly payment with these other values as the input it freezes as it was earlier no matter what.
That's because in that case, loanAmount * monthlyRate > monthlyPayment, so the new monthlyPayment value is negative, causing loanAmount to increase, never going below zero until it loops around due to integer overflow.