loop

I'm doing an assignment for school. The program is used to calculate how many months it would take to pay off a loan. The stuff after the loop is just temporary. This is my first time trying to work with loops and I really don't know what I'm doing haha. After the user input, the program hangs at the loop.


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
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
	int months, counter;
	double annualIntRate, loan, monthlyInt, monthlyPayment;

	cout << "Enter your loan amount: ";
		cin >> loan;
	cout << endl;

	cout << "Enter the annual interest rate: ";
		cin >> annualIntRate;
	cout << endl;

	cout << "Enter your monthly payment: ";
		cin >> monthlyPayment;
	cout << endl;
	months = 0;
    monthlyInt = (annualIntRate / 100) / 12;
	
	while (loan > 0)
	{
	monthlyPayment = monthlyPayment - loan * monthlyInt;
	loan = loan - monthlyPayment;
	months ++;
	}
	
	cout << loan << endl;

	cout << months << endl;
	
	
	system("PAUSE");
		return 0;
}
It looks here as if your using the while loop in the wrong context. The while loop is basically used to continuously repeat a sequence of events if the condition is true. I believe what you need here is the if function.

e.g:
1
2
3
4
5
6
7
8
9
10
if (age > 21){

            cout << "You're legal to drink. Please do so responsibly." << endl;
}

else {

            cout << "Your not old enough to drink!" << endl;

}
Last edited on
I need it to re-do those statements until loan is zero or less.
Check your formulas. You may be producing some negative/negligible numbers.
OK, there was a problem with the monthly payments. I fixed that so now I think that part of the program works. But I also have to make it output a warning if the monthly payment is too low. Not sure how to implement this. This is what I've got, if the payment is too low then the program hangs as before.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	if (monthlyPayment > intPerMonth)
		{
	while (loan > 0)
	{
	intPerMonth = monthlyPayment - loan * monthlyInt;
	loan = loan - intPerMonth;
	months ++;
	}
		cout << "It will take you " << months << " months to pay off your loan." << endl;
		}
		else
		cout << "You're not paying enough." << endl;
	cout << intPerMonth << endl;
	
	cout << loan << endl;
	
	system("PAUSE");
		return 0;
}
Last edited on
bump
Are you sure your formula is right from lines 3-5? I think you may want something closer to this:
1
2
// add the interest, then subtract the payment
loan = loan + (loan * monthlyInt) - monthlyPayment

As it is, it seems you are saying the monthly interest is the amount they are paying minus loan*monthlyInt. Are you sure that's right?
That may be a bit more streamlined heh. The way I had it still works though, I believe.

say the payment is 25, loan is 1000, monthly interest is .006
intPerMonth = 25 - (1000 * .006) = 19
loan = 1000 - 19 = 981

the way you said it is
loan = 1000 + (1000 * .006) - 25 = 981

I will go with the way you put it though since it looks better haha.
Last edited on
There was a problem with my declaration of the intPerMonth variable before the if statement, it works now. Thanks for trying to help, guys.

This is what I've got now:

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
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
	int months;
	double annualIntRate, loan, monthlyInt, monthlyPayment, intPerMonth;

	cout << "Enter your loan amount: ";
		cin >> loan;
	cout << endl;

	cout << "Enter the annual interest rate: ";
		cin >> annualIntRate;
	cout << endl;

	cout << "Enter your monthly payment: ";
		cin >> monthlyPayment;
	cout << endl;
	months = 0;
        monthlyInt = (annualIntRate / 100) / 12;
	intPerMonth = loan * monthlyInt;
	
	if (monthlyPayment > intPerMonth)
	{
		while (loan > 0)
		{
			loan = loan + (loan * monthlyInt) - monthlyPayment;
			months ++;
		}
		cout << "It will take you " << months << " months to pay off your loan." << endl;
	}
	else
		cout << "This monthly payment is too low to repay the loan." << endl;
	
	system("PAUSE");
		return 0;
}
Topic archived. No new replies allowed.