好的,这是我的代码
#include <stdio.h>
#include <string.h>
int main () {
char names[10][20];
int year[10], i, pos;
float mo[10], max=-1;
for (i=0; i<10; i++) {
do {
printf("Please enter your name:\n");
gets(names[i]);
if (strlen(names[i]) > 19)
printf("The name can contain a maximum of 19 characters!\n");
} while (strlen(names[i]) > 19);
do {
printf("Please enter your year of study:\n");
scanf("%d", &year[i]);
if (year[i]<1 || year[i]>5)
printf("Incorrect value! Accepted values are 1-5\n");
} while (year[i]<1 || year[i]>5);
do {
printf("Please enter your average score:\n");
scanf("%f", &mo[i]);
if (mo[i]<5 || mo[i]>10)
printf("Incorrect value! Accepted values are 5-10\n");
} while (mo[i]<5 || mo[i]>10);
if (mo[i]>max)
max=mo[i];
pos=i;
}
printf("The student with the highest score is %s with %f. His/Her year of study is %d\n", names[pos], max, year[pos]);
}
我知道我应该避免得到但这是我老师推荐的。 我也试过
scanf ("%[^\n]%*c", names[i]);
但是我遇到了同样的问题。它读取了第一个名字,但所有其他名称都没有。