2013-09-24 25 views
0

我有一個程序,其中有一個名爲students的10 struct變量的數組。在students我有一個char數組變量稱爲testAnswers與20個元素。我想要做的是將這十名學生的testAnswerschar數組變量answers與20個元素進行比較。基本上,變量answers是學生testAnswers的答題卡。答案都是真/假。這是我到目前爲止有:使用帶char和結構數組的strcmp時出錯

注:numStu是10和numAns是20

void checkAnswers(char answers[], student students[]){ 

for (int i = 0 ; i < numStu ; i++){ 
for (int d = 0 ; d < numAns ; d++){ 

if (students[i].testAnswers[d] == ' '){ 
      students[i].score += 1 ; //if the student did not answer the question add 1  which will be substracted once if loop sees it is not correct resulting in the   student losing 0 points.     
           } 
    if (strcmp(answers[d],students[i].testAnswers[d]) == 0){ 
       students[i].score +=2 ;//if the student answer is correct add 2 points to score 
       } 
    if (strcmp(answers[d],students[i].testAnswers[d]) != 0){ 
       students[i].score -= 1 ; //if the student answer is incorrect substrct 1 point 
       } 

}//end inner for 
}//end for outer 
    }//end checkAnswers 

我繼續錯誤接收:

invalid conversion from char to const char 
initializing argument 1 of `int strcmp(const char*, const char*)' 

對於這兩種情況下,我用strcmp 。我想知道是否有糾正這個錯誤或任何更好的方式來比較這兩個字符和評分測試。

回答

3

strcmp是要比較的(字符序列),而不是單個字符

你可以只使用一個平等的檢查單字符:

if (answers[d] == students[i].testAnswers[d]) 

請注意,如果我們談論的是布爾值,使用an explicit boolean type可能比char更好。

相關問題