infinite loop...write a program to tell you how many months..

You are to write a program to tell you how many months it will take to pay off any loan, as well as the total amount of interest paid over the life of the loan.

For example you have just purchased a stereo system that costs $1000 on the following credit plan: No down payment, an interest rate of 18% per year (and hence 1.5% per month), and monthly payments of $50. The monthly payment of $50 is used to pay the interest and whatever is left is used to pay part of the remaining debt. Hence, the first month you pay 1.5% of $1000 in interest. That is $15 in interest. So, the remaining $35 is deducted from your debt which leaves you with a debt of $965.00. The next month you pay interest of 1.5% of $965.00, which is $14.48. Hence, you can deduct $35.52 (which is $50 - $14.48) from the amount you owe.

Write a program that will tell you how many months it will take you to pay off any loan, as well as the total amount of interest paid over the life of the loan. Use a loop to calculate the amount of interest and the size of the debt after each month. Put out the monthly amount of interest paid and remaining debt. Use a variable to count the number of loop iterations and hence the number of months until the debt is zero. You may want to use other variables as well.

heres what i have done. Its giving me an infinite loop.. made changes, tested over and over again, but no luck
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
50
51
52
53
54
55
56
57
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <cmath>
using namespace std;
void main ()
{
	//declaring the variables
	double	totalLoan,
			balanceRemaining,
			principle,
			month,
			annualPercentage, 
			monthlyPayment, 
			monthlyInterest,
			interestPaid,
			principlePaid;
//sets fixed two deciomal point values through out the prog 
cout <<setprecision(2)<<fixed;
cout <<"What is the amount of the loan?";
cin >> totalLoan;
while (totalLoan < 0)
{
	cout <<"Invalid entry. Please enter a positive value only";
    cin >> totalLoan;
}
cout <<"What is the annual percentage rate?";
cin >> annualPercentage;
while (annualPercentage < 0)
{
	cout <<"Invalid entry. Please enter a positive value only";
    cin >> annualPercentage;
}
cout << "Enter your monthly payment: "<<endl;
cin >> monthlyPayment;
cout <<endl;
cout <<endl;
cout <<"Month"<<setw(15)<<"Principle"<<setw(13)<<"Interest"<<setw(13)<<"Principle"<<setw(13)<<"Remaining";
cout <<setw(50)<<"paid"<<setw(12)<<"paid"<<setw(17)<<"Balance\n";
monthlyInterest = (annualPercentage/(12*100));

//another while loop to fill up the body of the table until the paymentNumber is 11 or less.

for (double i = 0.0; i <= totalLoan; ++i)
{
	interestPaid= monthlyInterest*totalLoan;
	principlePaid= monthlyPayment-interestPaid;
	balanceRemaining=principle-principlePaid;
	principle=balanceRemaining+principlePaid;
	cout <<i<<setw(15)<<principle<<setw(13)<<interestPaid<<setw(13)<<principlePaid<<setw(13)<<balanceRemaining;

}


getch();

}

Last edited on
Main should return int, so don't write void main(), use int main().

Also, please, wrap your example in [.code] [./code] tags(without the dots). It's much simpler to read.
Hi, i have made the changes, can you have a look again please? thanks
Last edited on
Hi sda,

44
45
46
47
48
for (double i = 0.0; i <= totalLoan; ++i)
{
	interestPaid= monthlyInterest*totalLoan;
	principlePaid= monthlyPayment-interestPaid;
	balanceRemaining=principle-principlePaid;


Only use int in loop conditions. double are not stored exactly & this will cause problems one day.

With the loop - if my loan was $400,000 - the loop executes 400,000 times? Did you mean the number of payments?

Always initialise your variables to something - this is one of the biggest source of errors for beginners and some times not so beginners

Lines 22 - 33 could utilise a function bool IsPositive(double ANumber);

Good luck

hey thanks for all the help.

I made the appropriate changes in the loop, and changed up the formulas a little bit too in the loop. My calculator is giving me the right answer, but forget the loop, the whole program isn't working. And i have been working on this on and off for a couple of days. Please kindly help me



1
2
3
4
5
6
7
8
9
10
11
12
13
principle=totalLoan;
for (double i = monthlyPayment; i <= totalLoan; ++i)
{
    interestPaid= monthlyInterest*totalLoan;
	principlePaid= monthlyPayment-interestPaid;
	balanceRemaining=principle-principlePaid;
	principle=balanceRemaining+principlePaid;
	totalLoan = balanceRemaining;
	cout <<i<<setw(15)<<principle<<setw(13)<<interestPaid<<setw(13)<<principlePaid<<setw(13)<<balanceRemaining;

}
getch();
}


the compiler is giving me the following output:

All outputs are up-to-date.
1>ManifestResourceCompile:
1> All outputs are up-to-date.
1>LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.29
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on
Topic archived. No new replies allowed.