2013-11-02 27 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。

相關問題