assigning varibles within a loop in order to exit?

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.

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
#include "header.h"

void calculate (float loanAmount, float yearlyRate, float payment, float monthlyRate, int & months_to_repay, float monthlyPayment, float loan_after_MP) 

    
{


    monthlyPayment = loanAmount * monthlyRate;
    months_to_repay = 0;
    
    
    
    do 
      { 
        loan_after_MP = loanAmount -  monthlyPayment;
        
        loanAmount = loan_after_MP; // supposed to update loan amount
        
        monthlyPayment = loanAmount * monthlyRate; // supposed to update monthly payment amount
        
        months_to_repay += 1; //counts how number of loop executions.
      }          
    while (loanAmount > 0);  
     
    
}
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.


here is my modified code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "header.h"

void calculate (float loanAmount, float yearlyRate, float monthlyPayment, float monthlyRate, int & months_to_repay) 

    
{

    months_to_repay = 0;
    
    
    
    do 
      { 
        loanAmount -= monthlyPayment; // supposed to update loanAmount, thanks quirky
        
        monthlyPayment = (monthlyPayment - (loanAmount * monthlyRate)); // supposed to update monthly payment amount
        
        months_to_repay += 1; //counts how number of loop executions.
      }          
    while (loanAmount > 0);  
     
    
}
Last edited on
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.
Topic archived. No new replies allowed.