0

我有一个程序,其中有一个包含 10 个struct变量的数组,称为students. 在students我有一个用 20 个元素char调用的数组变量。testAnswers我要做的是将这十名学生testAnswers与一个名为20 个元素的char数组变量进行比较。answers基本上,变量answers是学生的答题卡testAnswers。答案都是真/假。这是我到目前为止所拥有的:

注:numStu是 10 和numAns20。

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. 我想知道是否有任何方法可以纠正这个错误,或者有什么更好的方法来比较这两个字符并为测试评分。

4

1 回答 1

3

strcmp是比较字符串(字符序列),而不是单个字符

您可以对单个字符使用相等检查:

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

请注意,如果我们谈论的是布尔值,使用显式布尔类型可能比char.

于 2013-09-24T22:34:13.080 回答