No output after "while" loop. WHATS WRONG?!?!

Hello, I am a newbie getting into programming and I am having an issue to get my "cout" to show after doing a while loop. I am not sure what I am doing wrong, I have seen this asked before and seem to be following the help listed, but for the life of me nothing is coming out. Can anyone help???

// Write a program that will tell you how many months it will take to pay off a loan.
// As well as the 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.

#include <iostream>
using namespace std;

int main()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

double Balance = 1000, Rate = 1.5, Payment = 50, TotalInterest = 0, InterestPaid = 0;
int count = 0;

cout << "$" << Balance << ", 18% annual interest (1.5% monthly), $50.00 monthly payments." << endl;

while ( Balance > 50 )
{
InterestPaid = Rate / 100 * Balance;
Payment = Payment - InterestPaid;
Balance = Balance - Payment;
TotalInterest = TotalInterest + InterestPaid;
count++;
}

cout << "$" << InterestPaid << endl;
cout << "$" << Payment << endl;
cout << "$" << Balance << endl;
cout << "$" << TotalInterest << endl;
cout << "It will take " << count << " months to pay off this purchase." << endl;
cout << "Total interest paid on this item: $" << TotalInterest << endl;

system ("pause");
return 0;
}
Last edited on
You should try the compound operators
ex:
+= , -= , *= , /= , %=

instead of Payment = Payment - InterestPaid;

you can do Payment -= InterestPaid;

Also it seems a bit redundant to do Payment -= InterestPaid
Then balance -= Payment

Why cant you just do balance -= InterestPaid?

Also technically speaking 18% annual interest is different than 1.5% monthly.

Say we start at 1000.

Annual(18%) = 820
Monthly(1.5%) = 834.13196834087708627557095727539

Since it does 1.5% interest on the current value and not the initial value.

Also I don't see your function doing what you say it should.

What you are saying is each month it is not paid off you owe an additional 1.5% and you are paying off 50$ a month

So say for example you start at 1000 dollars
that would mean

Month one you pay 50 so you owe 950$ then it isn't paid off for the first month so you now owe 964.25

Month two you pay off 50$ you now owe 914.25$ interest is added
you now owe 927.96375

Month three you pay off 50$ you now owe 877.97$ then they charge you the interest you now owe 891.13955

repeat...

I have made some code if this is what you are trying to do for your assignment ( I could be very wrong )

Maybe look something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main() {
	double loan = 1000.0;
	const double INTEREST = 0.015;
	const int PAYMENT = 50;
	int month = 0;
	
	while( loan > 0 )
	{
		loan -= PAYMENT;
		loan *= 1 + INTEREST;
		++month;
	}
	std::cout << "It took " << month << " months to pay off the loan." << std::endl;
	return 0;
}
It took 24 months to pay off the loan.


You might want to make it so say they owe 999.9123 dollars or w.e

then it rounds up a penny so they would really owe 999.92$

Banks are after all greedy for their money. They cant get their half pennies so they want a full penny.

*fixed a small typo



Last edited on
Following the example out of the book, my calculations are correct.

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.

So when I do the calculations once, all the numbers following this example come out correctly...well the $15/$35/$965...Only problem is after I add the "while loop", all of the "cout" information under the loop doesn't print out to the screen.
Last edited on
does your loop even end? It is written very weird. What happens if you have 30 dollars left? Do you not have to pay off that 30?

You have

1
2
3
4
5
6
7
8
while ( Balance > 50 )
{
InterestPaid = Rate / 100 * Balance;
Payment = Payment - InterestPaid; //problem is probably here..since it wont be a 50$ monthly payment anymore
Balance = Balance - Payment;
TotalInterest = TotalInterest + InterestPaid;
count++;	
}


Which can be simplified to

1
2
3
4
5
6
7
while( Balance > 50 ) //You probably want to put > 0 so if you have say 1 dollar or 30 dollar left you will be able to pay off still..
{
    InterestPaid = .015 * Balance;
    Balance -= payment - InterestPaid; //paying off interest then paying off some of the balance
    TotalInterest += InterestPaid;
    ++count;
}



As for my code

I had a few things mixed up but fixed em now tell me if this helps?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main() {
	double loan = 1000.0 , interest_paid = 0.0 , total_interest = 0.0;
	const double INTEREST = 0.015;
	const int PAYMENT = 50;
	int month = 0;
	
	while( loan > 0 )
	{
            interest_paid = loan * INTEREST;
            total_interest += interest_paid;
            loan -= PAYMENT + interest_paid;
            ++month;
	}
	std::cout << "It took " << month << " months to pay off the loan." << std::endl;
	std::cout << "You paid overal $" << total_interest << " in loans." << std::endl;
	return 0;
}
Success	 time: 0 memory: 3296 signal:0
It took 18 months to pay off the loan.
You paid overal $132.115 in loans.


http://ideone.com/eEvwnw

*edit indents didn't work for some reason.
Last edited on
Ok, yea I did set (Balance > 0) before checking again and it did better, but I also so did see the error in the calculation...THANKS!!
Topic archived. No new replies allowed.