Good Afternoon,

 I'm having problems with my school assignment that is about a loan program. Here are the details what the program needs to do. You are required to write a loan program for the Rio Grande Bank.  This program will print an
amortization table for a loan, given the amount of the loan, the annual interest rate, and the number of
payments.  This process will be repeated until the customer asks to quit.  The following is a list of the
tasks that your program has to complete. 

 Print a welcome message.
 Loop until the user wants to leave the program.  In this loop, the program executes the following 
three tasks. 
o An input section which asks for some information including the amount of the loan, the 
number of payments (in terms of months), and the annual interest rate. 
o A calculation section which calculates and prints an amortization table similar to the 
following for the input received from the first section. For example, given the amount of the
loan is 10000, the annual interest rate is 8% and the number of payments is 12, a table
similar to the following will be generated (may not be exact the same as yours due to
floating point errors).  



     Amortized Payment Schedule
            month    paid principal     paid interest    new balance
            1             803.22       66.67      9196.78
            2             808.57       61.31      8388.21
            3             813.96       55.92      7574.25
            4             819.39       50.49      6754.86
            5             824.85       45.03      5930.01
            6             830.35       39.53      5099.65
            7             835.89       34.00      4263.77 
            8             841.46       28.43      3422.31
            9             847.07       22.82      2575.24
            10           852.72       17.17      1722.52
            11           858.40       11.48        864.12
            12           864.12         5.76            0.00 


An output section to print out other results such as the monthly interest rate, the monthly 
payment, the accumulated interest payment over the loan period, and the total payment,
along with the input parameters. For the above example, the output section should display 
the following table:

         Customer:  John Doe
            loan amount:   10000.00
            annual interest rate:  8.00%
            number of payments: 12
            monthly interest:  0.6667% (Note: 4 decimal places!)
            accumulated interest: 438.61
            monthly payment :  869.89
            total payment : 10438.61 

To calculate and print the above tables, your program needs to calculate the monthly interest rate, the
monthly payment, and the interest portion of the payment. The following formulas are needed:

 Monthly interest rate  
        MonIntRate = (AnnualRate/100)/12

 MonthPayment = LoanAmount * MonIntRate * (1 + MonIntRate)^NumMonths/(1 + MonIntRate)NumMonths-1 

 MonthPaidInt = Balance * MonIntRate

 MonthPrincipal = MonthPayment - MonthPaidInt

 Balance = Balance - MonthPrincipal

At the end of the main loop, your program asks the customer if they would like to have another run of
the program.  The loop stops if the user selects to quit.  When the user quits, a farewell message should
be printed.  Both the amortization table and the final print-out (the total payment etc.) should be
recorded in an output file for you to hand in. 

Test data: 
Amount        $10,000 $10,000 $10,000 $20,000
Interest Rate 8%       7.5%    7%      7%
Months        12       12      24      24 

So far I have this code for the cpp file and header
The code for cpp file is:



    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>

    #include "hw3.h"
    using namespace std;

    int main()
    {
        string Fname, Lname;
        double loanAmount;
        double annualIntRate, MonIntRate=0;
        int No_of_Months;

        welcome();
        getCustomerInfo(Fname, Lname);
        getLongInfo(loanAmount, annualIntRate, No_of_Months);
        MonthlyInterestRateCalculator(annualIntRate);
        MonthlyPmtCalculator(annualIntRate, loanAmount, No_of_Months, MonIntRate);
        AmortizedTable(loanAmount, annualIntRate, No_of_Months);

        return 0;
    }

for the header file



    #include <iostream>
    #include <string>
    #include <iomanip>

    using namespace std;

    #ifndef         HW_3_HEAD_H
    #define         HW_3_HEAD_H

    void welcome()
    {
        cout << "Welcome to Rio Grande Bank "<<endl;
        cout << "We are please in assisting you in your banking needs" <<endl;

    }

    string getCustomerInfo(string Fname, string Lname)
    {
        string fullname;

        cout << "Enter your first name and last name:";
        cin>> Fname>>Lname;
        fullname = Fname + Lname;
        return fullname;
    }

    void getLongInfo(double& loanAmount, double& annualIntRate, int& No_of_Months)
    {

        cout << "Enter the loan amount that you wish to borrow:" <<endl;
        cin>>loanAmount;
        cout << "Enter the annual interest rate: ";
        cin>>annualIntRate;
        cout << "Enter the length of months of the loan ";
        cin>>No_of_Months;
    }

    double MonthlyInterestRateCalculator(const double annualIntRate)
    {
        double MonIntRate;
        MonIntRate = (annualIntRate/100)/12;

        return MonIntRate;
    }

    double MonthlyPmtCalculator(double annualIntRate, const double loanAmount, const double MonIntRate, const int No_of_Months)
    {
        double MonthPmt;
        MonthPmt = loanAmount * MonIntRate/(1 - pow(1 + MonIntRate, -No_of_Months));

        return MonthPmt;
    }

    void AmortizedTable(const double loanAmount, const double annualIntRate, const int No_of_Months)
    {
        double balance, monthlyPaidInt, MonthPrincipal, MonIntRate, MonthPmt, newBalance;

        MonIntRate = MonthlyInterestRateCalculator(annualIntRate);
        MonthPmt = MonthlyPmtCalculator(loanAmount, MonIntRate, No_of_Months, annualIntRate);

        balance = loanAmount;

        cout <<"\t Amortized Payment Schedule" <<endl;
        cout <<setw(6)<<left<<"Month"<<setw(12)<<left<<"Paid Principal"<<setw(14)<<right<<"Paid Interest"<<setw(12)<<right<<"New Balance"<<endl;

        for (int i=1; i <= No_of_Months; i++)
        {
            monthlyPaidInt = balance * MonIntRate;
            MonthPrincipal = MonthPmt - monthlyPaidInt;
            newBalance = balance - monthlyPaidInt;

            cout <<setw(6)<<left<<i<<setw(12)<<left<<MonthPrincipal<<setw(4)<<right<<monthlyPaidInt<<setw(14)<<right<<newBalance<<endl;
        }
    }

    #endif

And what seems to be the trouble with it? I can see that it is incomplete, but you will need to spell out just what you need help with.

Seriously, PoloBlue, you have been posting here long enough that you should know better than this by now. We are not mind readers; while we'll help you to the best of our abilities, we can't simply guess at the problems and hope to be right.

I can point out a few things, but that's about it. First off, you haven't actually implmented the main loop yet. The program runs through the sequence once, and stops, with no final message.

Second, the GetCustomerInfo() function returns a string, but you don't actually assign that string to a variable anywhere in main(); the customer information is simply getting lost. The same is true for MonthlyInterestRateCalculator() and MonthlyPmtCalculator().

Now, I don't know if you've covered structures in your class yet, but you might find it useful to declare the following struct types:

struct Customer
{
    string firstName, lastName;

};

struct LoanInformation 
{
    double loanAmount;
    double annualIntRate;
    int No_of_Months;
};

This helps organize the data and makes it easier to pass the data around between the functions.

As for the header issue, I've already said my piece about that, and see little point in re-hashing the issue with you; but as I said before, if you need help in setting up a project file to work with multiple compilation units, let me know and I'll walk you through it.

Schol-R-Lea,

The problem is that the program is not outputting the correct values for MonthPrincipal, monthlyPaidInt, and newBalance in the table. I think the formulas are not correct .

Thank you

Compare:

MonthPayment = LoanAmount * MonIntRate * (1 + MonIntRate)^NumMonths/(1 + MonIntRate)NumMonths-1

with

MonthPmt = loanAmount * MonIntRate/(1 - pow(1 + MonIntRate, -No_of_Months));

You should see that they don't match. The correct code should be

MonthPmt = loanAmount * MonIntRate * (pow(1 + MonIntRate, No_of_Months) / ((1 + MonIntRate) * No_of_Months)) - 1;    

Or at the very least, that is how I read it; I may be off still.

Edited 4 Years Ago by Schol-R-LEA

I did some looking around, and found that the loan payment formula you were given is wrong. The correct formula, as give here, is:

MonthPmt = loanAmount * (MonIntRate + (MonIntRate / (pow(1 + MonIntRate, No_of_Months) - 1)));

This still doesn't quite work right, however; I'll keep looking into it.

EDIT: I figured it out, all right. The problem was in your call to MonthlyPmtCalculator(); you had put the arguments out of order, with the Annual Percentage Rate last rather than first. The correct working code is as follows:

main.cpp

#include <string>
#include "hw3.h"

using namespace std;

int main()
{
    string Fname, Lname;
    double loanAmount;
    double annualIntRate, MonIntRate=0;
    int No_of_Months;
    welcome();
    getCustomerInfo(Fname, Lname);
    getLoanInfo(loanAmount, annualIntRate, No_of_Months);
    AmortizedTable(loanAmount, annualIntRate, No_of_Months);
    return 0;
}

hw3.h

#include <string>
#ifndef         HW_3_HEAD_H
#define         HW_3_HEAD_H

void welcome();
std::string getCustomerInfo(std::string& Fname, std::string& Lname);
void getLoanInfo(double& loanAmount, double& annualIntRate, int& No_of_Months);
double MonthlyInterestRateCalculator(const double annualIntRate);
double MonthlyPmtCalculator(double annualIntRate, const double loanAmount, const double MonIntRate, const int No_of_Months);
void AmortizedTable(const double loanAmount, const double annualIntRate, const int No_of_Months);

#endif

hw3.cpp

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include "hw3.h"


void welcome()
{
    std::cout << "Welcome to Rio Grande Bank "<< std::endl;
    std::cout << "We are please in assisting you in your banking needs" << std::endl;
}

std::string getCustomerInfo(std::string& Fname, std::string& Lname)
{
    std::string fullname;
    std::cout << "Enter your first name and last name:";
    std::cin>> Fname>>Lname;
    fullname = Fname + Lname;
    return fullname;
}

void getLoanInfo(double& loanAmount, double& annualIntRate, int& No_of_Months)
{
    std::cout << "Enter the loan amount that you wish to borrow:" <<std::endl;
    std::cin>>loanAmount;
    std::cout << "Enter the annual interest rate: ";
    std::cin>>annualIntRate;
    std::cout << "Enter the length of months of the loan ";
    std::cin>>No_of_Months;
}

double MonthlyInterestRateCalculator(const double annualIntRate)
{
    double MonIntRate;
    MonIntRate = (annualIntRate/1200);
    return MonIntRate;
}

double MonthlyPmtCalculator(double annualIntRate, const double loanAmount, const double MonIntRate, const int No_of_Months)
{
    double MonthPmt;
    MonthPmt = loanAmount * (MonIntRate + (MonIntRate / (pow(1 + MonIntRate, No_of_Months) - 1)));
    return MonthPmt;
}

void AmortizedTable(const double loanAmount, const double annualIntRate, const int No_of_Months)
{
    double balance, monthlyPaidInt, MonthPrincipal, MonIntRate, MonthPmt;
    MonIntRate = MonthlyInterestRateCalculator(annualIntRate);
    std::cout << "Monthly Interest Rate: " << std::setprecision(4) << MonIntRate * 100 << '%' << std::endl;
    MonthPmt = MonthlyPmtCalculator(annualIntRate, loanAmount, MonIntRate, No_of_Months);
    std::cout << "Monthly Payment: " << MonthPmt << std::endl;
    balance = loanAmount;
    std::cout <<"\t Amortized Payment Schedule" <<std::endl;
    std::cout << std::setw(6)  << std::left  << "Month"
              << std::setw(12) << std::left  << "Paid Principal"
              << std::setw(14) << std::right << "Paid Interest"
              << std::setw(12) << std::right << "New Balance"
              << std::endl;
    for (int i=1; i <= No_of_Months; i++)
    {
        monthlyPaidInt = balance * MonIntRate;
        MonthPrincipal = MonthPmt - monthlyPaidInt;
        balance = balance - MonthPrincipal;
        std::cout << std::setw(6)  << std::left  << i
                  << std::setw(12) << std::left  << std::setprecision(2) << std::fixed << MonthPrincipal
                  << std::setw(14) << std::right << std::setprecision(2) << std::fixed << monthlyPaidInt
                  << std::setw(14) << std::right << std::setprecision(2) << std::fixed << balance
                  << std::endl;
    }
}

Note that you still need to make a master loop in main(), and add the exit message. Still, this should get you well on your way.

Edited 4 Years Ago by Schol-R-LEA: FIXED!

Schol-R-Lea,

How can I fixed the following loop, so the each month have different values for paid principal, paid interest, and new balance. Not the same values for the whole 12 months.
This is the loop:

void AmortizedTable(const double loanAmount, const double annualIntRate, const int No_of_Months)
{
    double balance, monthlyPaidInt, MonthPrincipal, MonIntRate, MonthPmt, newBalance;

    MonIntRate = MonthlyInterestRateCalculator(annualIntRate);
    MonthPmt = MonthlyPmtCalculator(loanAmount, MonIntRate, No_of_Months, annualIntRate);

    balance = loanAmount;

    cout <<"\t Amortized Payment Schedule" <<endl;
    cout <<setw(6)<<left<<"Month"<<setw(12)<<left<<"Paid Principal"<<setw(14)<<right<<"Paid Interest"<<setw(12)<<right<<"New Balance"<<endl;

    for (int i=1; i <= No_of_Months; i++)
    {
        monthlyPaidInt = balance * MonIntRate;
        MonthPmt = 869.89;
        MonthPrincipal = MonthPmt - monthlyPaidInt;
        newBalance = balance - MonthPrincipal;
        cout << fixed <<showpoint<<setprecision(2);
        cout <<setw(6)<<left<<i<<setw(12)<<left<<MonthPrincipal<<setw(10)<<right<<monthlyPaidInt<<setw(14)<<right<<newBalance<<endl;
    }
}
This question has already been answered. Start a new discussion instead.