Hi guys, I'm supposed to make a program that calculates how many months it would take to pay off your credit card balance with a 14% yearly interest rate. I've written out what I believe to be it but I end up getting this.


The program doesn't seem to continue after typing in the balance, or rather seems to be stuck in an infinite loop. Can anyone explain why this is happening?

Edit- After looking more closely, it seems the problem is that the second while loop never ends since it's never greater than 12. Would that be the problem?

Edit2- It seems I fixed it with a simple AND statement.

Edit3- On trying the program out, it seems to have the same problem when you type in a value higher than 1900...


# include <iostream>

using namespace std;

int main()

        int payment = 20;
        int month = 0;
        float balance;
        float interest;
        int counter = 0;
          //Ask what their balance is.
        cout << "How much money did you waste?" << endl;
        cin >> balance;
          //Calculate the time needed to pay off the balance.
        while (balance > 0)
                while (counter < 12 && balance > 0)
                  ++ month;
                  ++ counter;
                  balance = balance - payment;
                        if (counter == 12)
                          counter = 0;
                          interest = balance * .14;
                          balance = balance + interest;
                          balance = balance;
        cout << "It would take you " << month << " months to pay off your "
             << "balance." << endl;

Recommended Answers

All 2 Replies

If you have more than 1900 of debt at 14%, and you only pay 20 per month, you will never be able to repay your debt, ad infinitum. That's why you get an infinite loop. You should add a check at the start of the loop to see if the yearly payment is at least bigger than the interest accrued before going into an infinite loop (or add a check to see if the balance is actually decreasing from year to year).

Also, the loop at line 17 is redundant to the one at line 19. I think that just removing the one that starts at line 19 would work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.