while loop not working

this is the problem:
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 the 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.

my code is:
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
#include <iostream>
using namespace std;
int main()
{
    char x;
    double total_int_paid, pay_duration, total_months, remaining_debt, cost=1000;
    int c=0; //c is the count down of months
    const int monthly_pay=50;
    const double monthly_interest=.015;
    
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    
    cout<<"You brought a stereo system that is $1000 with a 1.5% interest per month and \n";
    cout<<"monthly payments of $50\n";
    cout<<"stereo system is $1000\n"<<"monthly interest is 1.5%\n"<<"monthly payment is $50\n";
    while (remaining_debt<50)
    {
    remaining_debt=cost-(monthly_pay-(cost*monthly_interest));

    c++;
    }
    cout<<remaining_debt<<endl;
    cout<<c;
    cin>>x;
    
    return 0;
}


i didn't finish yet because my while loop isn't working. i want the loop to keep looping until the remaining debt is less than 50 but it just does 1 loop. i also want to count how many loops i did and c is the counter. when i run it, it gives me 965.00 for the remaining debt and 1 for the c(how many times it looped).
how do i fix this?
Last edited on
When you enter the while loop `remaining_debt' hasn't been assigned a value. Just initialize it to 0 when you declare it.
do you mean like this?

1
2
3
4
5
6
7
double remaining_debt=0;
while (remaining_debt<50)
{
remaining_debt=cost-(monthly_pay-(cost*monthly_interest));

c++;
}


i still get the same results
remaining_debt doesn't have a value because you have to use cost-(monthly_pay-(cost*monthly_interest)) to get the value.
Last edited on
Remaining debt should be set to cost, should it not?

Additionally, there is no increment in the calculation. It simply does the same calculation with the same numbers, over and over.
Sorry I missed this before:

i want the loop to keep looping until the remaining debt is less than 50 but it just does 1 loop.


`while(remaining_debt < 50)' will keep running while remaining_debt is below 50, meaning if you set remaining_debt to more than 50 it will jump out of the loop which seems the opposite of what you want. See if this does what you want:

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
#include <iostream>
using namespace std;
int main()
{
    char x;
    double total_int_paid, pay_duration, total_months, remaining_debt, cost=1000;
    int c=0; //c is the count down of months
    const int monthly_pay=50;
    const double monthly_interest=.015;
    
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    
    cout<<"You brought a stereo system that is $1000 with a 1.5% interest per month and \n";
    cout<<"monthly payments of $50\n";
    cout<<"stereo system is $1000\n"<<"monthly interest is 1.5%\n"<<"monthly payment is $50\n";
    remaining_debt = cost; // starting debt is full price of stereo
    while (remaining_debt>50)
    {
      remaining_debt -= monthly_pay - (remaining_debt*monthly_interest); // subtract monthly payment, add interest
      c++;
    }
    cout<<remaining_debt<<endl;
    cout<<c;
    cin>>x;
    
    return 0;
}
thanks!! it worked :)
Topic archived. No new replies allowed.