0

用户输入一个双精度和字符串,该字符串存储到两个数组中。像这样:

{
                 double DoC;
                 string Nm;
                 cout << " Please enter the amount charged to your credit card " << endl;
                 cin >> DoC;
                 cout << " Please enter where the charge was made " << endl;
                 cin >> Nm;
                 getline(cin, Nm);
                 cca.doCharge(Nm,DoC);
                 break;
                 }

然后将 double 和 string 传递给 this :

{
    if (amount > 0)
    {
        for (int i = 9; i != 0; i--)
        { 
            last10withdraws[i] = last10withdraws[i-1];
            last10charges[i] = last10charges[i-1];
        }

        last10withdraws[0] = amount;
        last10charges[0] = name;

        setBalanceW(amount);  
    }
    else
    {
        cout << " ERROR. Number must be greater then zero. " << endl;
    }
    return 0;
}

这似乎非常适合将数据存储到数组中。但是,然后我使用此函数显示数组内部的数据:

{
    cout << " Account: Creditcard Withdraws " << " " << " Account: Creditcard Deposits " << " " << " Account: Creditcard last 10 charges " << endl;
    cout << "            " << last10withdraws[0] << "                       " << last10deposits[0] << "                       " << last10charges[0] << endl;
    cout << "            " << last10withdraws[1] << "                       " << last10deposits[1] << "                       " << last10charges[1] << endl;
    cout << "            " << last10withdraws[2] << "                       " << last10deposits[2] << "                       " << last10charges[2] << endl;
    cout << "            " << last10withdraws[3] << "                       " << last10deposits[3] << "                       " << last10charges[3] << endl;
    cout << "            " << last10withdraws[4] << "                       " << last10deposits[4] << "                       " << last10charges[4] << endl;
    cout << "            " << last10withdraws[5] << "                       " << last10deposits[5] << "                       " << last10charges[5] << endl;
    cout << "            " << last10withdraws[6] << "                       " << last10deposits[6] << "                       " << last10charges[6] << endl;
    cout << "            " << last10withdraws[7] << "                       " << last10deposits[7] << "                       " << last10charges[7] << endl;
    cout << "            " << last10withdraws[8] << "                       " << last10deposits[8] << "                       " << last10charges[8] << endl;
    cout << "            " << last10withdraws[9] << "                       " << last10deposits[9] << "                       " << last10charges[9] << endl;
    cout << endl;

}

假设用户在存款数组中输入了三个双精度数。当我调用该函数来显示它时,我得到如下信息:

60
30
20
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061

我怎样才能使 -9.25596e+061 为 0?我一直无法找到任何真正帮助我的东西。同样对于包含字符串的数组,当它被调用来显示时,它什么也不显示。为什么是这样 ?

4

2 回答 2

2

将数组初始化为:

int last10withdraws[10] = {0};

现在所有元素都为零。

如果用户输入三个数字,那么前三个元素将为非零(假设只允许非零),其余元素将为零。

如果last10withdraws是类 m 的成员(并且您使用的是 C++03),那么您可以使用成员初始化器列表进行默认初始化,这会将所有元素初始化为零。

class myclass
{
      int last10withdraws[10];
   public:
      myclass() : last10withdraws() 
      {       //^^^^^^^^^^^^^^^^^^^ member initializer list
      }
};

希望有帮助。

于 2013-04-18T02:52:04.327 回答
0

您收到错误是因为您没有初始化这些值。首先说int last10withdraws[10] = {0};这会将所有值初始化为零。

于 2013-04-18T03:03:10.890 回答