简体   繁体   中英

I keep getting unitialized variable used for interestRate, numberOfPayments, and loanAmount

Sarah Connors is fName and lName, 10000 is loanAmount 12 interestRate and 3 is the number of years for the loan, this is all located in "Lab4Data.txt"

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

using namespace std;

int main()
{
    string fName, lName;
    
    double interestRate, loanAmount, monthlyPayment, totalPayed, interestAmount,

    double numberOfPayments

    ifstream input;

    ofstream output;
    
    input.open("Lab4Data.txt");
    output.open("Kirklandlab4results.txt");
    interestRate = interestRate / 12;
    interestRate = interestRate / 100;
    numberOfPayments = numberOfPayments * 12;
    
    monthlyPayment = loanAmount * interestRate
            * pow(loanAmount + interestRate, numberOfPayments)
            / pow(loanAmount + interestRate, numberOfPayments) - 1; //equation to get monthly payment
    totalPayed = monthlyPayment * numberOfPayments;
    interestAmount = interestRate * numberOfPayments;
    
    input >> fName >> lName;
    output << "Name:" << fName << " " << lName;
    
    input >> loanAmount;
    output << "Loan Amount:" << loanAmount << endl;
    
    input >> interestRate;
    output << "Monthly Intrest Rate:" << interestRate << endl;
    
    input >> numberOfPayments;
    output << "Number Of Payments:" << numberOfPayments << endl;
    
    output << "Monthly Payment:" << monthlyPayment << endl;
    output << "Total Amount Payed:" << totalPayed << endl;
    output << "Interest Amount:" << interestAmount << endl;
    
    output.close();
    
    return 0;
}

im trying to figure out why its telling me that im getting unitialized variable used for interestRate, numberOfPayments, and loanAmount. I am new to C++ and need help.

In these statements

interestRate = interestRate/12;
interestRate = interestRate / 100;
numberOfPayments = numberOfPayments * 12;
monthlyPayment = loanAmount * interestRate;

there are used uninitialized variables

double interestRate, loanAmount, monthlyPayment, totalPayed, interestAmount, 
    
double numberOfPayments;

that invokes undefined behavior.

For example what is the value of the variable interestRate in this expression interestRate/12 ? As the variable was not initialized (neither value was assigned to it in its declaration) it contains some garbage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM