You are here:

C++/Help with value returning function

Advertisement


Question
I am writing a program to print out the payments of a car loan. In my program i am having trouble with my CalcMonPayment function. i think im putting too much information into the function or my for loop is errored. any help would b greatly appreciated

#include<iomanip>
#include<cmath>
using namespace std;

struct input
{
   float price;
  float downPayment;
  float tradeIn;
  float loanAmt;
  float annualIntRate;
  float annualIntPercent;
  float monIntRate;
  int noMonths;
  float monPayment;
};

//function prototypes
double CalcMonPayment(input inputdata, double[]);
void DisplayLoanSchedule(int&, double[]);

int main()
{
   //declare variables
   float price; //vehicle price
   float tradeIn; //trade-in value
   float downPayment; //down-payment
   double annualIntRate; //Annual interest rate
   double monIntRate; //Monthly interest rate
   double annualIntPercent; //Annual interest percentage
   float loanAmt; //Loan Amount
   double monPayment[60]; //Monthly Payment 1-d array
   int noMonths; //where no Months - 24, 36, 48, 60

  //Functions
   float getPrice();
   double getInterestRate();
   float getDownPayment(float, float);
   float getTradeIn(float);

  struct input inputdata; //create an instance of "input" named "inputdata"
   
  loanAmt = 0.00;
   inputdata.price = getPrice();
   
   inputdata.tradeIn = getTradeIn(inputdata.price);
   inputdata.downPayment = getDownPayment(inputdata.price, inputdata.tradeIn);

   inputdata.annualIntRate = getInterestRate();
  
   CalcMonPayment(inputdata, monPayment);

   annualIntPercent = inputdata.annualIntRate * 100.0;

   cout << setw(50) << "Honest Dave's Used Cars";
  cout << endl;
  cout << endl;

   cout << setw (30) << "Vehicle Price" << fixed << setprecision(2) << setw(20) << inputdata.price << endl;
   cout << setw (30) << "Trade In Value" << fixed << setprecision(2) << setw(20) << inputdata.tradeIn << endl;
   cout << setw (30) << "Down Payment" << fixed << setprecision(2) << setw(20) << inputdata.downPayment << endl;
   cout << setw (50) << " _________________________" << endl;
   cout << setw (30) << "Loan Amount" << fixed << setprecision(2) << setw (20) << loanAmt << endl << endl;

   cout << setw (30) << "Annual Interest Rate" << fixed << setprecision(2) << setw(20) << annualIntPercent << "%" << endl << endl;
   cout << setw (50) << "Monthly Payment Options \n\n";

   DisplayLoanSchedule(noMonths, monPayment);

   cin.get();
   cin.get();
}


   //Get the price of the vehicle with do/while loop and return by value
   //Price must be greater than $50.00 and less than $50,000.00
float getPrice()
{
   float price;
   do
   {
       cout << "Enter the Price of the Vehicle: ";
       cin >> price;
       if (price > 50000.00 || price < 50.0)
         cout << "Please re-enter a price between $50.00 and $50000.00" << endl;
   }while (price > 50000.00 || price < 50.0);

   return price;
}

   //Get the trade in value with do/while loop and return by value
   //tradeIn must be greater than or equal to zero and less than the price
float getTradeIn(/* in */ float price)
{
   float tradeIn;
   do
   {
       cout << "Enter the Value of the Trade-In: ";
       cin >> tradeIn;
       if (tradeIn < 0 || tradeIn >= price)
       {
         cout << "Please re-enter a POSTIVE value which is less" << endl;
         cout << "than the Vehicle Price " << endl;
       }
   }while (tradeIn <0 || tradeIn >= price);
   
   return tradeIn;
}

   //Get the down payment with do/while loop and return by value
   //value is greater than or equal to zero and less than price minus the trade-in
float getDownPayment(/* in */ float price, /* in */ float tradeIn)
{
   float downPayment;
   do
   {
       cout << "Enter the Down-Payment: ";
       cin >> downPayment;
       if (downPayment < 0 || downPayment > (price - tradeIn))
       {
         cout << "Please re-enter a POSITIVE Down-Payment which is less" << endl;
         cout << "than the Vehicle Price minus the Trade-In" << endl;
       }
   }while (downPayment < 0 || downPayment > (price - tradeIn));

   return downPayment;
}

   //Get the interest rate with do/while loop and return by value
   //Must be greater than zero and less than .50
double getInterestRate()
{
   double annualIntRate;
   do
   {
       cout << "Enter the Annual Interest Rate (0.00 - 0.49): ";
       cin >> annualIntRate;
       if (annualIntRate < 0 || annualIntRate >= 0.50)
         cout << "Please re-enter a number between 0.00 and 0.49";
   } while (annualIntRate < 0 || annualIntRate >= 0.50);

   return annualIntRate;
}

   //Calculate the monthly payments and store into a 1-d array.  Must be a return by value function
   //Calculate the monthly payments @ 24, 36, 48, & 60 months
double CalcMonPayment(input inputdata, double monPayment[])
{
  int noMonths = 24;
  double monIntRate = inputdata.annualIntRate / 12.0;
  double loanAmt = inputdata.price - inputdata.downPayment - inputdata.tradeIn;
  int count;

   for (count = 0; count <= 12; count++)
   {
     monPayment[count] = (loanAmt * monIntRate) / (1.0 - pow((monIntRate + 1), -noMonths));
   }
   
   return monPayment[];
}

   //Display the Loan Schedule. Pass variables using a struct and four payments as an arrray
   //Use a for loop to display the four monthly payments.  Must be a void function
void DisplayLoanSchedule(/* in */ int& noMonths, double monPayment[])
{
  noMonths = 24;
  int count;
   for (count = 0; count <= 3; count++)
   {
       cout << setw (30) << noMonths << " Months" << fixed << setprecision(2) << setw(20) << monPayment[noMonths] << endl;
       noMonths = noMonths + 12;
   }

   return;
}

Answer
I don't think you're doing too much in CalcMonPayment.  Since it modifies the incoming pointer with data, it need not return a value (in fact in the call you didn't assign the return value).

The only thing I see that is suspicious is:

for (count = 0; count <= 12; count++)

This is going to run 13 times - I bet you want

for (count = 0; count < 12; count++)

to go 12 months.  Otherwise, things look pretty good and you've done OK here.

Bill

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.