2013-09-24 30 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更好。

相关问题