2013-01-21 27 views
0
if (sale->taxStatus[i] = "y") // line 44 

收率錯誤:比較C++炭結構爲ASCII值產生錯誤

y.cpp:44:12: error: request for member taxStatus in sale, which is of non-class type Sale*

我的結構:

struct Sale { 
    int quantity[MAX_SALES]; 
    float unitPrice[MAX_SALES]; 
    char taxStatus[MAX_SALES]; // MAX_SALES = 10 
}; 

全功能:

void total(struct Sale sale[], int sales) { 

    int i = 0; 
    float subTotal, hst, total = 0; 

    for (i = 0; i < sales; i++) { 
     subTotal = subTotal + (sale->quantity[i] * sale->unitPrice[i]); 
     if (sale->taxStatus[i] = "y") 
     { 
      hst = hst + ((sale->quantity[i] * sale->unitPrice[i]) * 0.13); 
     } 
    } 

    cout << "\n" << "Subtotal : " << subTotal << endl; 
    cout << "HST (13%) : " << hst; 
} 
+0

在第44行中使用'=='作爲等式,'='作爲賦值。 –

+2

與''y''而不是''y' 。雙引號用於字符串文字。字符是單引號的。 – jweyrich

+0

啊,我認爲這是相反的。現在我得到這個錯誤:'ISO C++禁止指針和整數之間的比較'行44:29 – eveo

回答

4
if (sale->taxStatus[i] == 'y') 

由於@jweyrich和@AustinPhillips也指出你需要雙等號(==),並且字符在單引號中進行比較。

單等號(=)用於分配值。

例如s = 5;

雙等號(==)用於檢查兩個或兩個以上值的相等性。

例如if(s == d && d== e && e == f && f == b) { };

帶有感嘆號(!=)的等號用於檢查兩個或兩個以上值的不等式。

例如if(s != d && d != e && e != f && f != b) { };

+0

謝謝,菜鳥的錯誤。 – eveo

+0

我是C++的新手hahah:P –

+0

@JesseGood編輯得很好,我屬於web開發背景,新加入'c'和'C++':D –

0

首先,如前所述if (sale->taxStatus[i] = "y")是分配而不是比較!

但主要問題是:sale(函數參數)是一個數組(大小爲sales),但你訪問它(裏面for體)作爲Sale* ...

正確的訪問必須是sale[i]->taxStatus因爲索引i指向數組sale,而不是taxStatus! (順便說一句,如果sales > MAX_SALES你會得到UB)。那麼你需要使用strcmp"y"文字進行比較......

+0

1)你說的是數組訪問權限; 2)他爲'sale-> taxStatus'中的每個位置存儲單個字符('y' /'n'),因此使用'strcmp'沒有意義。但是,目前還不清楚他的意圖是什麼。有一個'Sale'數組似乎更適合於IMO(因此不需要這些數組中的數組)。 – jweyrich

+0

@jweyrich:那麼沒有理由將'taxStatus [MAX_SALES]'作爲數組... – zaufi

+0

我正在更新我的評論。請重新閱讀。 – jweyrich