【发布时间】:2018-10-24 11:25:30
【问题描述】:
当我尝试编译我的程序时,我在标题中收到警告消息,当我在扫描名称和评分后运行它时,它就停止了。我在练习使用字符串时遇到了很多次这个问题,但我一直无法找到解决方案。
#include <stdio.h>
struct students {
char name[20];
int score[20];
} student;
int main() {
int i, n;
printf("Number of students:\n");
scanf("%d", &n);
for(i=0; i<n; i++) {
printf("Name of the student:\n");
scanf("%s", &student.name[i]);
printf("Score of the student:\n");
scanf("%d", &student.score[i]);
}
for(i=0;i<n;i++) {
if(student.score[i] >= 15) {
printf("%s passed the exam\n", student.name[i]); }
else {
printf("%s failed the exam\n", student.name[i]);
}
}
return 0;
}
【问题讨论】:
-
你知道错误是在哪一行抛出的吗?如果您从
scanf中删除&会发生什么情况? -
一个 char 是单个字符。一个字母,数字左右。您有 20 个字符的空间用于 20 名学生的姓名。
-
用一个名字由多个字符和一个分数组成一个单数
struct student。然后制作一个学生数组s。 -
第 19 和 21 行。
-
@scylla0120 - 要解决特定评论,您可以输入“@”,后跟用户名字。例如:
@B. Cratty在评论框中,它会标记您已回复的用户。