Thread: consumer loan program errors

  1. #1
    Registered User helloalyssa's Avatar
    Join Date
    Sep 2010
    Posts
    25

    consumer loan program errors

    (taken from the assignment page)

    "Your program will ask the user to enter the amount of money they want to borrow, the interest rate, and the monthly payment amount. Your program will then determine how many months it will take to pay off that loan, what the final payment amount will be, and how much interest was paid over that time. If the monthly payment amount isn't high enough to pay off more than one month's interest, your program should notify the user what the minimum monthly payment is. "


    i'm getting errors that all three variables in the getInput(principal, interestRate, monthlyPayment); function line are all being used before their values are set.

    i'm also getting errors that say payLoan(principal, monthlyInterest, monthlyPayment, months, total_Interest); function monthlyInterest and total_Interest are being used before their values are set also.

    my last error says that for the line monthlyInterest = interestRate / month; interestRate is being using before its value is set.


    i know these errors are easy fixes but i've been trying for a couple hours to fix them and i'm really frustrated. i feel dumb for not being able to figure it out myself! argh. if anyone could give me a hand that would be great. =]



    here is my code:

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    const int month = 12;      // global constant for amount of months 
    
    void getInput(double, double, double);
    double payLoan(double, double, double, int, double);
    
    int main()
    {
      double principal,         // amount user wants to borrow 
    	 interestRate,	    // annual interest rate (as a percent)
    	 monthlyPayment,    // amount user has to pay per month 
    	 monthlyInterest,   // amount of interest user has to pay per month
    	 total_Interest;    // total amount of interest user has to pay 
      int months = 1;           // total amount of months loan will be repaid in
    
      cout << "** Welcome to the Consumer Loan Calculator **";
    
      getInput(principal, interestRate, monthlyPayment);
      payLoan(principal, monthlyInterest, monthlyPayment, months, total_Interest);
    
      cout << "** Don't get overwhelmed with debt! **";
    
    return 0;
    
    }
    
    
    
    //**************************************************************************
    // This functions asks the user how much they want to borrow, the annual   *
    // interest rate (expressed as a percent) and how much the user has to pay *
    // monthly. 								   *
    //**************************************************************************
    
    void getInput(double principal, double interestRate, double monthlyPayment)
    {
      do
      {
        cout << "How much do you want to borrow?";
        cin >> principal;
      } while (principal <= 0);
        cout << "You must enter a positive number! \n";
    
      do
      {
        cout << "What is the annual interest rate expressed as a percent?";
        cin >> interestRate;
      } while (interestRate <= 0);
    
      do
      {
        cout << "What is the monthly payment amount?";
        cin >> monthlyPayment;
      } while (monthlyPayment <= 0);
        cout << "You must enter a positive number! \n";
    }
    
    
    //***************************************************************************
    // This function calculates how many months it will take for the loan to be * 
    // paid off, how much the final payment will be and the total amount of     *
    // interest to be paid over the course of the repayment period.             *
    //***************************************************************************
    
    double payLoan(double principal, double monthlyInterest, double monthlyPayment, int months, double total_Interest)
    {
      double interest,
    	 payment,
    	 interestRate,
    	 finalPayment,
    	 leastPayment;
    
      cout << fixed << showpoint << setprecision(2);
    
      monthlyInterest = interestRate / month;
    
      //int months = 1;
      while (months++)
      {
        while (principal > monthlyPayment)
        {
          interest = principal * monthlyInterest;
          payment = monthlyPayment - interest;
          if (payment < 1)
          {
            leastPayment = interest + 1;
    	cout << "You must make payments of at least $" << leastPayment << endl;
            cout << "Because your monthly interest is $" << interest << endl;
          }
          principal = principal - payment;
          total_Interest += interest;
         }
         if (principal < monthlyPayment)
         break;
      }
      finalPayment = principal + principal * interest;
      total_Interest += interest;
    
      cout << "Your debt will be paid off after" << months << "months, "; 
      cout << "with a final payment of just $" << finalPayment << endl;
      cout << "The total amount of interest you will pay during that time is $" << total_Interest << endl;
    
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    When I compile your code one of the warning messages I get is:

    main.cpp|77|warning: ‘interestRate’ is used uninitialized in this function

    Code:
    double payLoan(double principal, double monthlyInterest, double monthlyPayment, int months, double total_Interest)
    {
       double interest,
       payment,
       interestRate,
       finalPayment,
       leastPayment;
    
       cout << fixed << showpoint << setprecision(2);
    // The following line is line 77
       monthlyInterest = interestRate / month;
    You never initialize the variables used in the function. At the marked line interestRate has not been initialized to any value.

    So where is interestRate supposed to get it's value?

    Should it be a constant set to a value when declared?

    Should it be passed to the function from main?

    NOTE: You should always initialize your variable when you declare them.

    ie: int someValue = 0;

    Also this is not the only error, take a look at your getInput() function, test the values after returning from the function.

    Jim
    Last edited by jimblumberg; 11-16-2010 at 05:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help to solve program errors
    By pattop in forum C++ Programming
    Replies: 6
    Last Post: 05-28-2006, 01:57 AM
  2. mega compile errors for small program
    By s_UNI_ in forum C++ Programming
    Replies: 4
    Last Post: 04-28-2005, 12:00 PM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. I'm a newbie and I can't understand the errors in my program
    By iluvmyafboys in forum C++ Programming
    Replies: 19
    Last Post: 02-20-2002, 10:40 AM