You are here:

C++/Program Errors

Advertisement


Question
I finished writing this program some hours ago. I compiled it and a long list on errors came up. I read the book tried to follow the book and went through the notes from the professor and I can not figure out how to solve these problems. Can you help me because I thought I did everything right until the compiler showed otherwise here is my program:

/****************************************************************
Program: carLoanProg.cpp

Modified: 4/16/09
Description: This program reads in in financial input from a
customer and determines if a customer qualifies for a car loan,
and output whether the customer is eligible or not and also
output the calculated monthly payment.
***************************************************************/

#include <iostream>
#include <math.h>
using namespace std;

//Declared functions
void getInput(double & costOfCar, double &downPayment, int &annualInterestRate,  int &lifeOfLoan, double &annualIncome);
double calcMonthlyPayment(double &carCost, double &downPay, int &annualRate, int &loanLife);    
bool userQualified(double monthlyPayment, double yearlyIncome);

int main()
{
   bool status;
  

   getInput(costOfCar, downPayment, annualInterestRate, lifeOfLoan, annualIncome);
  
  calcMonthlyPayment(carCost, downPay, annualRate, loanLife);
  
  status = userQualified(monthlyPayment, yearlyIncome);
  
     if (status == true)
     {
        cout << "You qualify for the loan" << endl;
     }
     else
        cout << "You do not qualify for the loan" << endl;
         
   cout << "Your monthly payment is " << monthPayment << endl;

return 0;
}

//This function gets the user's input
void getInput(double &costOfCar, double &downPayment, int &annualInterestRate, int &lifeOfLoan, double &annualIncome)
{
  do
  {
   cout << "Enter the cost of the car: " << endl;
   cin >> costOfCar;
      
   cout << "Enter the down payment amount you will make: " << endl;
   cin >> downPayment;
      
    cout << "Enter the annual interest rate: " << endl;
    cin >> annualInterestRate;
      
    cout << "Enter your annual income: " << endl;
    cin >> annualIncome;
      
    cout << "Enter the number of years for the life of the loan: " << endl;
    cin >> lifeOfLoan;
   }while (costOfCar > 0 && downPayment > 0 && annualInterestRate > 0 && annualIncome > 0 && lifeOfLoan > 0); //Gets input from user as long as it is greater than zero.
}

//This function calculates the monthly payment
double calcMonthlyPayment(double &carCost, double downPay, int &annualRate, int &loanLife)
{
   double monthPayment;
  double principal;
  double j;
  int n;
  
  //monthly interest in decimal form.
  j = annualRate / (12 * 100);
  
  //number of months over which loan is amortized.
  n = loanLife * 12;
  
  //the amount of the loan
  principal = carCost - downPay;
  
monthPayment = principal * (j / (1 - (pow(1+j, -n))));
  
}

//This function determines the user's eligibility for the loan.
 bool userQualified(double &monthlyPayment, double &yearlyIncome)
 {
     bool qualify;
   double monthlyIncome;
   
   
   &yearlyIncome = annualIncome;
   &monthlyPayment = monthPayment;
   
   monthlyIncome = yearIncome / 12;
   
   if (monthlyIncome * 4 >= monthlyPayment * 4)
   {
     qualify = true;
   }
   else
      qualify = false;
     
  return qualify;
  }

These are the compile errors:
carLoanProg.cpp:25: error: `costOfCar' undeclared (first use this function)
carLoanProg.cpp:25: error: (Each undeclared identifier is reported only once for each function it appears in.)
carLoanProg.cpp:25: error: `downPayment' undeclared (first use this function)
carLoanProg.cpp:25: error: `annualInterestRate' undeclared (first use this function)
carLoanProg.cpp:25: error: `lifeOfLoan' undeclared (first use this function)
carLoanProg.cpp:25: error: `annualIncome' undeclared (first use this function)
carLoanProg.cpp:27: error: `carCost' undeclared (first use this function)
carLoanProg.cpp:27: error: `downPay' undeclared (first use this function)
carLoanProg.cpp:27: error: `annualRate' undeclared (first use this function)
carLoanProg.cpp:27: error: `loanLife' undeclared (first use this function)
carLoanProg.cpp:29: error: `monthlyPayment' undeclared (first use this function)
carLoanProg.cpp:29: error: `yearlyIncome' undeclared (first use this function)
carLoanProg.cpp:38: error: `monthPayment' undeclared (first use this function)
C:UsersLeniseDesktopLenise C++ ProgramscarLoanProg.cpp: In function `bool userQualified(double&, double&)':
carLoanProg.cpp:93: error: `annualIncome' undeclared (first use this function)
carLoanProg.cpp:94: error: `monthPayment' undeclared (first use this function)
carLoanProg.cpp:96: error: `yearIncome' undeclared (first use this function)
carLoanProg.cpp:114:2: warning: no newline at end of file

any help would do I'm so puzzled right now this is my first computer program course and I have been doing good up until this point.

Thanks for taking time out of your day.

Answer
Lenise,

Ok, the errors are all the same and an easy fix.  All of the names in main have to be declared (in main).  Simply add declarations for all the parameters you passed in the functions called from main and it should compile and probably run.  Otherwise, you're off to a good start - good job on this!

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.