2013-11-02 21 views
1

所以,我得到了上面的错误(在标题中),但由于某种原因,它只是在第二个循环中抛出这个错误。注意我使用customer变量的第一和第二循环非常好,没有错误或任何错误。但在最后一个循环中,输出[customer] [charge]数组,在输出[customer]下面有一个红线,表示“下标值不是数组,指针或向量”。我使用xcode,Mavericks OSX。我所有的数组都是在其他地方定义的,并且直到现在,它们完美地完成了程序的整个长度。程序中还有一些其他操作正在进行,但它们与此循环无关,所以我只是发布了提供错误的代码。我再说一次,收费[客户] [月] [收费]循环正常工作,但输出[客户] [输出]不起作用。下标值不是数组,指针或向量,C++

P.S.你可能会认为保持数字索引数组中所有这些数据的逻辑是愚蠢的,但它是一个学校项目。所以不要告诉我这个程序在逻辑上如何不一致或者什么。谢谢!

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; 
    } 
} 

回答

4

里面的for声明:

for(int output=0; output<5; output++) 
{ 

你声明的另一个变量int output其与for语句外的同名阴影的double output[3][5]

2

这是你的问题:

double output[3][5]; 
for(int output=0; output<5; output++) 

你重用output作为变量名的两倍。

所以,当你试图在这里访问:

cout << output[customer][output] << endl; 

你访问本地output,这只是一个int。

相关问题