为什么这个代码姓名不能输入三个及以上个汉字?
姓名不能输入过多字符,输入过多字符时程序会结束
c
include
include
include
include
struct Student //自定义结构体 { int number; char name[100]; int jidao; int math; int eng; int com; }stu[40]; int main() //输入学生信息 { int n = 0; int i = 0; char c; do { printf("\t\t\t学号:"); scanf s("\t\t\t%d", &stu[n + i].number); printf("\t\t\t姓名:"); scanf s("\t\t\t%s", &stu[n + i].name,sizeof(50)); printf("\t\t\t计导分数:"); scanf s("\t\t\t%d", &stu[n + i].jidao); printf("\t\t\t高数分数:"); scanf s("\t\t\t%d", &stu[n + i].math); printf("\t\t\t外语(日语/英语)分数:"); scanf s("\t\t\t%d", &stu[n + i].eng); printf("\t\t\tC语言分数:"); scanf s("\t\t\t%d", &stu[n + i].com); i++; printf("\t\t\t继续录入成绩吗?\n"); printf("\t\t\t请输入‘y’继续,或's'停止"); scanf_s("\t\t\t%c", &c,10); } while (c == 'y'); return (i + n); } ```
-
scanf s("\t\t\t%s", &stu[n + i].name,sizeof(50)); 改为 scanf s("%s", &stu[n + i].name,100);
你不能用sizeof(50)啊,sizeof(50)的结果相当于sizeof(int),而不是50啊。既然你name数组长度是100,这里用100就好了啊 输入语句不需要写\t的
-
printf("\t\t\t姓名:"); scanf s("\t\t\t%s", stu[n + i].name, sizeof(stu[0].name)); //scanf s("\t\t\t%s", &stu[n + i].name, sizeof(50));
发表回复