enter your name: li A B 1 enter your name: wang A A -53为什么这里只能输入两次答案,我给了循环4次???

李小嘉 发布于 2015/11/08 13:06
阅读 155
收藏 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


struct student result(struct student stu);
void display_bast(struct student *stu);


char answer[4] = {'A', 'B', 'C','D'};
struct student
{
char score;
char name[32];
};


int main(int argc, char const *argv[])
{
struct student stu[3];
int i = 0;
while(i < 3)
{
printf("enter your name:\n");
fgets(stu[i].name, 32, stdin);
stu[i] = result(stu[i]);
i++;
}


display_bast(stu);
return 0;
}


struct student result(struct student stu)
{
int i;
char ch;
for(i=0; i<4; i++)
{
ch = getchar();
stu.score += ((ch == answer[i]) ? 1 : 0);
}
printf("%d\n", stu.score);
return stu;
}
void display_bast(struct student *stu)
{
int i;
struct student best = stu[0];
for(i=1; i<3; i++)
{
if(best.score < stu[i].score)
best = stu[i];
}
printf("%s: %d\n", best.name, best.score);
}
加载中
0
莫扎特的代码
莫扎特的代码
struct student result(struct student stu)
{
int i;
char ch;
stu.score=0;			//变量要初始化!!!
for(i=0; i<4; i++)
{
ch = getchar();
getchar();				//读取空格或者换行,如果不要这句,直接输ABCD中间不要有其他字符
stu.score += ((ch == answer[i]) ? 1 : 0);
}
printf("%d\n", stu.score);
return stu;
}



返回顶部
顶部