basic over view>> banks offer car loans for periods ranging from two to five years (24 to 60 months). Write a program to allow the customer to enter the price of a car, the down payment amount and the annual interest rate of the loan. The program should display the loan’s duration in months and the monthly payments for two-, three-, four-, and five- year loans. The output should be displayed in fixed-point notation, rounded to two decimal places of precision. payment =(rate*(pow(1 + rate),n) / ((pow(1 +rate),n)- 1)) * L. Rate is the monthly interest rate, N is the number of payments and L is the amount of the loan.
PROBLEM>>i'm getting: uninitialized local variable 'loanDuration' used. in what way can i fix this?

#include <iostream>
#include <iomanip>
#include <cmath>  // For pow function
using namespace std;

int main()
{
        double carPrice, downPayment, annual_rate, loan_amount;
        double monthly_payment1, monthly_payment2, monthly_payment3, monthly_payment4;
        double monthly_payment1A, monthly_payment2B, monthly_payment3C, monthly_payment4D;
        double monthly_rate, loanDuration, k;

        //rate = MONTHLY interest rate
        //N = number of payments
        //L = amount of the loan

        //ask user to enter the price of the car
        cout << "enter the price of the car: ";
        cin >> carPrice;

        //ask user to enter the downpayment amount
        cout << "enter the down payment amount: ";
        cin >> downPayment;

        //ask user to enter the annual interest rate of the loan
        cout << "enter the annual(per year) interest rate of the loan: ";
        cin >> annual_rate;

        //loanDuration = numPayments (N)

        loan_amount = carPrice - downPayment;
        monthly_rate = annual_rate / loanDuration;
        
        cout << "----------------------------------------------\n";
        cout << setw(5) << "Monthly Payments\n";
        
        //payment =(rate*(pow(1 + rate),n)  / ((pow(1 +rate),n)- 1)) * L
        
        k = 1 +  monthly_rate;
        monthly_payment1 = ((monthly_rate * pow(k, 24)) / (pow(k, 24) - 1)) * loan_amount;
        cout << setw(10) << "2 yrs: " <<  monthly_payment1 << endl;
   
        monthly_payment2 = ((monthly_rate * pow(k, 36)) / (pow(k, 36) - 1)) * loan_amount;
        cout << setw(10) << "3 yrs: " <<  monthly_payment2 << endl;
   
        monthly_payment3 = ((monthly_rate * pow(k, 48)) / (pow(k, 48) - 1)) * loan_amount;
        cout << setw(10) << "4 yrs: " <<  monthly_payment3 << endl;
   
        monthly_payment4 = ((monthly_rate * pow(k, 60)) / (pow(k, 60) - 1)) * loan_amount;
        cout << setw(10) << "5 yrs: " <<  monthly_payment4 << endl;
        cout << "----------------------------------------------\n";

        //loan's duration in months: 2-5 yrs(24 to 60 months)
        cout << "enter loan duration in months\n";
        cin >> loanDuration;
        cout << "loan's duration is: " << loanDuration << "months\n";

        //2-3 years
        if (loanDuration >= 24 && loanDuration <= 35)
        {
                monthly_payment1A = ((monthly_rate * pow(k, loanDuration)) / (pow(k, loanDuration) - 1)) * loan_amount;
                cout << "monthly payments for 2 years are: " << monthly_payment1A << endl;
        }
        //3-4 years
        else if (loanDuration >=36  && loanDuration <= 47)
        {
                monthly_payment2B = ((monthly_rate * pow(k, loanDuration)) / (pow(k, loanDuration) - 1)) * loan_amount;
                cout << "monthly payments for 3 years are: " << monthly_payment2B << endl;
        }
        //4-5 years
        else if (loanDuration >= 48  && loanDuration < 60)
        {
                monthly_payment3C = ((monthly_rate * pow(k, loanDuration)) / (pow(k, loanDuration) - 1)) * loan_amount;
                cout << "monthly payments for 4 years are: " << monthly_payment3C << endl;
        }
        //5 years
        else if (loanDuration == 60)
        {
                monthly_payment4D = ((monthly_rate * pow(k, loanDuration)) / (pow(k, loanDuration) - 1)) * loan_amount;
                cout << "monthly payments for 5 years are: " << monthly_payment4D << endl;
        }
        else 
                cout << "The bank offers car loans for periods ranging only from 2-5 years (24 to 60 months)\n";


   return 0;
}

Line 11 loanDurationon is defined without giving it an initial value

Line 29 the code what would have given loanDurationon its initial value is commented out

Line 32 loanDurationon is used in a calculation having nenver been given a value because line 29 is commented out.


You need to assign a value to loanDurationon before execution reaches line 32.

I don't see an error message posted nor a line number pointed to. You might want to give us ALL the details, not just some of them.

oh sorry bout that....i went through it 4 more times and discovered the problem tho. but i appreciate the reply! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.