Compiler gives no errors, yet it doesn't continue past a certain point when I run it

idk what im doing wrong here.

so here's the problem

Write a program that accepts as input:

The loan amount
The interest rate per year
The monthly payment.

(Enter the interest rate as a percentage. For example, if the interest rate is 7.2% per year, then enter 7.2.)

The program then outputs the number of months it would take to repay the loan.

here's my code:

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    double loanAmount, interestRate, payment, monthlyRate, intAmount, firstMonth;
    int month = 0;
    
    
    cout << "Please enter the loan amount: ";
    cin >> loanAmount;
    
    cout << "Please enter the interest rate per year: ";
    cin >> interestRate;
    
    monthlyRate = interestRate / 12;
    
    cout << "Please enter the monthly payment: ";
    cin >> payment;
    cout << endl;
    
    
    firstMonth = (monthlyRate / 100) * loanAmount;
    cout << showpoint << setprecision(2) << fixed; 
    
    if (payment > firstMonth)
    {
    	do
    	{
	intAmount = (monthlyRate / 100) * loanAmount;
    	payment = payment - intAmount;
    	loanAmount = loanAmount - payment;
    	month++;	
	}
	while (loanAmount != 0);
	cout << "It will take " << month << " months to pay it back." << endl;
     }
    else
    {
     cout << "Monthly payment is too low. The loan cannot be repaid." << endl;
    }
	
	
	
	system("pause");
	return 0;
    
}
Last edited on
You don't say what's wrong.

When I try to compile your program, I get an error at line 27. The ; is missing at the end of the statement.
But it's an if statement, if i put ; at the end of the if then it will ignore everything else. I'm not getting an error on line 27 in my compiler. Im using Dev c++ if youre wondering.

-- i just tried putting ; at the end of the if statement, i get an error because the else isn't paired with an if now.

edit; also for some reason the spacing on here is really bad, even though its not the same way in my compiler. So i tried manually fixing it the best way I could since copying + pasting keeps giving it some extra and weird spaces in random places.
Last edited on
> for some reason the spacing on here is really bad
you are mixing tabs and spaces


while (loanAmount != 0)
you'll have precision issues, instead of 0 you may reach a really small number but your loop will keep going
change it to while(loadAmount > 0)
switched it to that, but when I run it i still get the same problem where it doesn't tell me how many months it will take, it just sits there and blinks at me taunting me, daring me to put my fist through the monitor.
The reason your program never ends and never prints out the final result is because your do-while loop never ends. You are looking for loan amount == 0.0, but that never occurs because of 1 of 2 reasons.

First, you are reducing payment every time through the while loop. That means the monthly payment is constantly being reduced. You really want to change payment in lines 32 and 33 to "principalReduction".

1
2
double principalReduction = payment - intAmount;
loanAmount = loanAmount = principalReduction;


Second, in line 33 you only want to subtract principalReduction from loanAmount if it is actually less than loanAmount. Otherwise, just set loanAmount to 0.0.
Thank you, Doug.

It has been fixed, it is working with no errors and instead of setting loanAmount to 0.0, I just did loanAmount > 0 which worked correctly.

Thank you for also teaching me, I was getting very angry at this simple solution lol
But it's an if statement, if i put ; at the end of the if then it will ignore everything else. I'm not getting an error on line 27 in my compiler.

You changed the code in your original post.
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    double loanAmount, interestRate, payment, monthlyRate, intAmount, firstMonth;
    int month = 0;


    cout << "Please enter the loan amount: ";
    cin >> loanAmount;

    cout << "Please enter the interest rate per year: ";
    cin >> interestRate;

    monthlyRate = interestRate / 12;

    cout << "Please enter the monthly payment: ";
    cin >> payment;
    cout << endl;


    intAmount = (monthlyRate / 100) * loanAmount;
    cout << showpoint << setprecision(2) << fixed;

    firstMonth = (loanAmount - (payment - intAmount))
//...................................................^ semicolon missing.  Line subsequently removed.
        if (payment > firstMonth)
        {
            do
            {
                payment = payment - intAmount;
                loanAmount = loanAmount - payment;
                month++;
            } while (loanAmount != 0);
            cout << "It will take " << month << " months to pay it back." << endl;
        }
        else
        {
            cout << "Monthly payment is too low. The loan cannot be repaid." << endl;
        }



    system("pause");
    return 0;

}
You keep decreasing the payment. Eventually it drops to zero, then it becomes negative, which means you start adding to the loan amount instead of subtracting from it.
Please enter the loan amount: 1000
Please enter the interest rate per year: 4.5
Please enter the monthly payment: 50

Month 1:        payment=46.25   loan amount=953.75
Month 2:        payment=42.67   loan amount=911.08
Month 3:        payment=39.26   loan amount=871.82
Month 4:        payment=35.99   loan amount=835.83
Month 5:        payment=32.85   loan amount=802.98
Month 6:        payment=29.84   loan amount=773.14
Month 7:        payment=26.94   loan amount=746.19
Month 8:        payment=24.14   loan amount=722.05
Month 9:        payment=21.44   loan amount=700.61
Month 10:       payment=18.81   loan amount=681.80
Month 11:       payment=16.25   loan amount=665.55
Month 12:       payment=13.76   loan amount=651.79
Month 13:       payment=11.31   loan amount=640.48
Month 14:       payment=8.91    loan amount=631.57
Month 15:       payment=6.54    loan amount=625.03
Month 16:       payment=4.20    loan amount=620.83
Month 17:       payment=1.87    loan amount=618.96
Month 18:       payment=-0.45   loan amount=619.41
Month 19:       payment=-2.77   loan amount=622.18
Month 20:       payment=-5.11   loan amount=627.29
Month 21:       payment=-7.46   loan amount=634.75

Topic archived. No new replies allowed.