Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts

    counting error c++

    Hello all,

    I am working on a program which calculates loan information. For some reason the program keeps looping even though I have set the loop variables to stop when month count gets to 60 or the balance is a 0 or less than. Looking for some hints.

    Thanks
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    double loanbalance;
    double interestrate;
    double payment;
    double newloanamt;
    int monthnum= 0;
    int i=1;
    float fBalance;
    
    
    
    
    
    int main ()
    {
    cout << "Starting loan balance: $";
    cin >> loanbalance;
    cout << "Annual interest rate: %";
    cin >> interestrate;
    cout << "Monthly payment: $";
    cin >> payment;
    
    
    
    
    cout << " \nMonth\tBalance" << endl;
    while (monthnum < 60 || loanbalance<=0){
    monthnum = monthnum + 1;
    loanbalance = loanbalance*(interestrate/12) + loanbalance - payment;
    
    cout << monthnum << "\t" << loanbalance << endl;
    
    
    }
    
    
    return 0;
    }

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    It would be pretty easy to determine the problem if you run your code in a debugger. That should always be the first step to perform when your application is not working as expected. When I help people with their code I first do a quick glance over the code to see if it is something obvious. If I don't see it in the first glance I compile it and run it in a debugger so I can step through the code and watch the values of each variable. </rant>
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •