在C ++中,if子句中的变量未初始化

时间:2015-02-04 01:13:30

标签: c++ if-statement

我想成为一名初学者,并将此代码编写为项目的一部分。根据loanCode,我想为变量设置不同的值并将其打印出来。但是,该程序只是忽略了“if”子句中的初始化。 谁知道什么是错的?谢谢!

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;


int main()

{



cout << "This program can be used for new and existing loans!" << endl;


int loanLength;
int paymentPerYear;
double interestRate;
int maxAmount;
int minAmount;

string borrowerName;
cout << "Please enter the borrower's full name: " << endl;
getline (cin,borrowerName);

int loanCode;
cout << "Please enter a BRSL loan code: " << endl;
cin >> loanCode;


if (loanCode == 10)
{
    int loanLength = 30;         // (30-year plan)
    int paymentPerYear = 12;     // (12 payments per year)
    double interestRate = .038;  // (Interest Rate is 3.8%)
    int maxAmount = 425000;      // (Maximum amount is $425000)
    int minAmount = 5000;        // (Minimum amount is 5000)
    cout << "ok 10" << endl;
}

else if (loanCode == 20)
{
    int loanLength = 15;         // (15-year plan)
    int paymentPerYear = 12;     // (12 payments per year)
    double interestRate = .029;  // (Interest Rate is 2.9%)
    int maxAmount = 425000;      // (Maximum amount is $425000)
    int minAmount = 5000;        // (Minimum amount is 5000)
    cout << "ok 20" << endl;
}

else if (loanCode == 30)
{
    int loanLength = 5;          // (5-year plan)
    int paymentPerYear = 12;     // (12 payments per year)
    double interestRate = .026;  // (Interest Rate is 2.6%)
    int maxAmount = 100000;      // (Maximum amount is $100000)
    int minAmount = 5000;        // (Minimum amount is 5000)
    cout << "ok 30" << endl;
}

else if (loanCode == 40)
{
    int loanLength = 2;          // (2-year plan)
    int paymentPerYear = 12;     // (12 payments per year)
    double interestRate = .072;  // (Interest Rate is 7.2%)
    int maxAmount = 25000;       // (Maximum amount is $25000)
    int minAmount = 500;         // (Minimum amount is 500)
    cout << "ok 40" << endl;
}

else
{
    cout << "Loan code is not a valid value." << endl;
    cout << "Program will exit now" << endl;
    return 0;
}


cout << "your loan code is: " << loanCode << endl;
cout << "loanLength: "        << loanLength << endl;
cout << "paymentPerYear: "    << paymentPerYear << endl;
cout << "interestRate: "      << interestRate << endl;
cout << "maxAmout: "          << maxAmount << endl;
cout << "minAmount: "         << minAmount << endl;


return 0;
}

输出:

此计划可用于新贷款!

请输入借款人的全名:

Joe Shmoe

请输入BRSL贷款代码:

10

ok 10

您的贷款代码是:10

loanLength:0

paymentPerYear:0

interestRate:0

maxAmout:0

minAmount:0

程序以退出代码结束:0

3 个答案:

答案 0 :(得分:1)

{
    int loanLength = 2;          // (2-year plan)
    int paymentPerYear = 12;     // (12 payments per year)
    double interestRate = .072;  // (Interest Rate is 7.2%)
    int maxAmount = 25000;       // (Maximum amount is $25000)
    int minAmount = 500;         // (Minimum amount is 500)
    cout << "ok 40" << endl;
}

{...}打开一个新范围。在范围内声明的任何新变量都只存在于该范围内。

你想要的是分配到范围内的现有变量

{
    loanLength = 2;          // (2-year plan)
    paymentPerYear = 12;     // (12 payments per year)
    interestRate = .072;  // (Interest Rate is 7.2%)
    maxAmount = 25000;       // (Maximum amount is $25000)
    minAmount = 500;         // (Minimum amount is 500)
    cout << "ok 40" << endl;
}

......等等。

答案 1 :(得分:1)

“初始化”是您在创建或“声明”变量时执行的操作。你根据C ++的规则在你的程序中正确地执行它,但是你的程序没有做到你认为它正在做的事情 - 从说话的方式 - 你可能会说你做错了! / p>

基本上,您声明和初始化的所有变量都是不同的。 它们是不同的变量。它们具有相同的名称,但无关紧要,因为它们存在于不同的范围

您必须声明变量(如果您愿意,将它们初始化为0之前 if语句(您已经这样做了),然后在里面, 只需分配新值

要分配给现有变量,只需写下:

variableName = value;

type variableName = value;

最后,我强烈,强烈,强烈,强烈地强烈建议您启用编译器警告。我不理解为什么人们不使用编译器警告。如果你启用了编译器警告,你的编译器会告诉你这个问题,因为它很聪明地发现这段代码几乎肯定不是你想写的。

答案 2 :(得分:0)

您已经在&#39; if&#39;之外声明了您的变量。语句,而不是这个:

int loanLength = 30;

这样做:

loanLength = 30;