因此,我收到了上述错误(在标题中),但出于某种原因,它只在第二个循环中抛出此错误。请注意,我使用客户变量的第一个和第二个循环工作得非常好,没有抛出任何错误或任何东西。但是在最后一个循环中,output[customer][charge] 数组,在 output[customer] 下有一条红线,上面写着“下标值不是数组、指针或 vector ”。我正在使用 xcode,Mavericks OSX。我的所有数组都在别处定义,并且直到现在在整个程序长度内都完美运行。程序中还有一些其他操作在进行,但它们与这个循环无关,所以我只是贴出了出错的代码。我再说一遍,charges[customer][month][charge] 循环工作正常,但 output[customer][output] 不工作。
附言您可能会认为将所有这些数据保存在数字索引数组中的逻辑很愚蠢,但它是针对学校项目的。所以不要跟我说这个程序在逻辑上不一致或其他什么。谢谢!
string headings[3][7];
string chargeLabels[3] = {"Electricity :","Water: ","Gas: "};
string outputLabels[5] = {"Subtotal: ","Discount: ","Subtotal: ","Tax: ","Total: "};
double charges[3][3][3];
double output[3][5];
for(int customer=0; customer<3; customer++)
{
for(int heading=0; heading<5; heading++)
{
cout << headings[customer][heading];
}
for(int month=0; month<3; month++)
{
cout << chargeLabels[month];
for(int charge=0; charge<3; charge++)
{
cout << charges[customer][month][charge] << ", ";
}
cout << endl;
}
for(int output=0; output<5; output++)
{
cout << outputLabels[output];
//error is below this comment
cout << output[customer][output] << endl;
}
}
最佳答案
在 for
语句中:
for(int output=0; output<5; output++)
{
您声明了另一个变量 int output
,它在 for
语句之外隐藏了具有相同名称的 double output[3][5]
。
关于c++ - 下标值不是数组、指针或 vector ,C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19743896/