用户输入一个double和string,它存储在两个数组中。像这样:
{
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传递给:
{
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?我找不到真正帮助我的任何东西。对于包含字符串的数组,当它被调用以显示它时,它什么都不显示。这是为什么?
答案 0 :(得分:2)
将数组初始化为:
int last10withdraws[10] = {0};
现在所有元素都为零。
如果用户输入三个数字,则前三个元素将为非零(假设仅允许非零),其余元素将为零。
如果last10withdraws
是m类的成员(并且您正在使用C ++ 03),则可以使用member-initializer list进行default-initialize,这会将所有元素初始化为零。 / p>
class myclass
{
int last10withdraws[10];
public:
myclass() : last10withdraws()
{ //^^^^^^^^^^^^^^^^^^^ member initializer list
}
};
希望有所帮助。
答案 1 :(得分:0)
您收到错误是因为您没有初始化值。首先说int last10withdraws[10] = {0};
这会将所有值初始化为零。