我制作了以下程序来输入学生的证书,然后打印出来。但是当我完成输入两个学生的记录(允许的最大记录输入数)时,它不会打印它们。 这是程序。任何人都可以指出错误吗?谢谢。
#include<stdio.h>
#include<string.h>
struct student
{
char name[20];
char mobile_no[10];
char class[5];
};
int main()
{
static struct student s[2];
int m=0,n=0,i;
char c;
printf("Enter the name , mobile_no and class of the students\n");
while((scanf("%c",&s[m].name[n]))!= EOF)
{
for(n=0; n<=19; n++)
scanf("%c",&s[m].name[n]);
for(n=0; n<=9; n++)
scanf("%c",&s[m].mobile_no[n]);
for(n=0; n<=4; n++)
scanf("%c",&s[m].class[n]);
scanf("%c",&c); //scans for the newline character \n
n = 0;
m++;
}
for(i=0 ; i<m ; i++)
{
printf("%s%3s%3s\n",s[i].name,s[i].mobile_no,s[i].class); //prints the structure
}
}
答案 0 :(得分:-1)
我不做家庭作业,但你很幸运。记住,你输入的时间不能超过结构字符串中的声明 - 1.自己审查,释放等等。
struct student
{
char name[20];
char mobile_no[10];
char _class[5];
};
int read_students(struct student *s)
{
int m = 0;
printf("Enter the name , mobile_no and class of the students. Name == exit for exit\n");
while (1)
{
char tmp[20];
printf("Name: ");
scanf("%s", tmp);
if (!strcmp(tmp, "exit")) break;
if ((s = (struct student *)realloc(s, sizeof(struct student) * (m + 1))) == NULL) {m = -1; break;}
strcpy(s[m].name, tmp);
printf("Mobile: ");
scanf("%s", s[m].mobile_no);
printf("Class: ");
scanf("%s", s[m]._class);
m++;
}
if (m != -1)
{
printf("Number of students: %d\n", m);
for (int i = 0; i<m; i++)
{
printf("%s %3s %3s\n", s[i].name, s[i].mobile_no, s[i]._class);//prints the structure
}
}
return m;
}
并在主要功能中:
struct student *s = NULL;
int numberofstudentds;
numberofstudentds = read_students(s);