views:

56

answers:

1

Hi everyone! I created a program that calculates loans, but it doesn't go under the guidelines of what my professor asked. Can show me the correct alteration. Source Code would be awesome and time saving, but you don't have to.

Heres the problem:

Write a program that lets the user enter the loan amount and the loan period in number of years and displays the monthly and total payments for each interest rate starting from 5% to 8%, with an increment of 1/8. Heres a sample run:

Loan amount: 10000 [Enter]
Numbers of Years: 5: [Enter] 

Interest rate          Monthly Payment                     Total Payment 
5%                         188.71                              11322.74
5.125%                     189.28                              11357.13

Heres my previous code:

# include <iostream>
# include <iomanip>
# include <cmath>

using namespace std;

int main ()
{
    double loanAmountA;
    double annualRate;
    double paymentAmount;
    double amountInterest;
    double ratePeriod;
    double balanceAfter;
    double amountApplied;
    double balance;
    double paymentPeriod;
    int paymentsPerYear;
    int totalPayments;
    int loanCount = 1;
    int paymentCount = 1;
    bool anotherLoan = true;
    char response;

    while (anotherLoan == true)
    {
        cout<<"Enter amount of loan A:$ ";
        cin>>loanAmountA;
        cout<<endl;
        cout<<"Enter annual percentage rate (APR): "<<"%";
        cin>>annualRate;
        cout<<endl;
        cout<<"Enter the number of payments per year: ";
        cin>>paymentsPerYear;
        cout<<endl;
        cout<<"Enter the total number of payments: ";
        cin>>totalPayments;
        cout<<endl;
        cout<<"Payment Payment Amount Amount to Balance after";
        cout<<endl;
        cout<<"Number Amount Interest Principal This Payment";
        cout<<endl;

        cin.ignore(80,'\n');

        while (paymentCount <=totalPayments)
        {
            annualRate = annualRate / 100;
            balance = loanAmountA - totalPayments * paymentAmount;
            ratePeriod = balance * annualRate;
            paymentAmount = loanAmountA * (totalPayments / paymentsPerYear * annualRate) / totalPayments;
            balanceAfter = balance - paymentAmount;
            balance = loanAmountA - (paymentCount * paymentAmount);


            cout<<left<<setprecision(0)<<setw(3)<<paymentCount;
            cout<<setw(13)<<left<<fixed<<setprecision(2)<<paymentAmount;
            cout<<setw(26)<<left<<fixed<<setprecision(2)<<ratePeriod;
            cout<<setw(39)<<left<<fixed<<setprecision(2)<<balance;
            cout<<setw(42)<<left<<fixed<<setprecision(2)<<balanceAfter;

            if (paymentCount % 12 == 0)
            {
                cout<<endl;
                cout<<"Hit <Enter> to continue: "<<endl;
                cin.ignore(80,'\n');
                cin.get();
            }
            paymentCount++;
            loanCount++;
            cout<<endl;
        }

        cout<<"Would you like to calculate another loan? y/n and <enter>";
        cin>>response;
        if (response == 'n') 
        {
            anotherLoan = false;
            cout<<endl<<endl;
            cout<<"There were"<<loanCount<< "loans processed.";
            cout<<endl<<endl;
        }
    }
    return 0;
}
+1  A: 

Did you try to use the debugger, and find the point of failure?

Igor Oks
For Linux there is `ddd`, which is cool.
Hamish Grubijan