What am I doing wrong with this loop

Page 1 of 1

2 Replies - 387 Views - Last Post: 04 September 2012 - 03:32 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=290966&amp;s=fbdeee06dd305d8ef96235050cd21e39&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 zachkt

Reputation: 0
• Posts: 41
• Joined: 19-July 12

What am I doing wrong with this loop

Posted 04 September 2012 - 03:04 PM

I am trying to write a program that tells me how many months it will take to pay off a loan, and the total interest that will be paid. When i run this, it doesnt tell me the final number of months, instead it just gives me the total interest at month 1, month 2 etc.

```
#include <iostream>
using namespace std;
int main ()
{
double debt = 100;
double interest;
double pay;
double totalinterest = 0;
int month = 0;

cout << " You have bought a stereo that costs \$1000 with the following credit plan:" << endl;
cout << endl;
cout << " No down payment, an interest rate of 18% per year and monthly payments of \$50." << endl;
cout << endl;

while (debt>0)
{
interest = debt * .015;
pay = 50 - interest;
debt = debt - pay;
month = month + 1;
totalinterest = totalinterest + interest;

cout << "It will take you" << month << "months to pay off the debt." << endl;
cout << "The total interest paid in the end will be \$" << totalinterest << endl;
}

return 0;

}

```

Is This A Good Question/Topic? 0

Replies To: What am I doing wrong with this loop

#2 jimblumberg

Reputation: 4912
• Posts: 15,350
• Joined: 25-December 09

Re: What am I doing wrong with this loop

Posted 04 September 2012 - 03:22 PM

One of the issues is that you have assigned debt a value of 100 instead of 1000. The other is that your cout statements should probably be after the loop, not inside the loop.

Jim

#3 zachkt

Reputation: 0
• Posts: 41
• Joined: 19-July 12

Re: What am I doing wrong with this loop

Posted 04 September 2012 - 03:32 PM

jimblumberg, on 04 September 2012 - 03:22 PM, said:

One of the issues is that you have assigned debt a value of 100 instead of 1000. The other is that your cout statements should probably be after the loop, not inside the loop.

Jim

You're the man! I actually had the cout statement outside the loop before i posted this but it wasnt giving me any result. The "100" was definitely the issue. Thank you