我们被要求用 c 编写一个特定的程序,使用户能够
- 添加学生
- 查看所有学生
- 搜索学生并退出
使用结构。该程序应该像学生的门户一样。我有这个“暂定代码”,在编译时会打印出分段错误(核心转储)的错误。所以这就是我的代码的运行方式:
#include<stdio.h>
typedef struct tag1{
int day, year, month;
}Date;
typedef struct tag2{
int number;
char name[50];
char course[30];
Date birthday;
}Record;
main(){
int choice, n, i=0;
Record student[200];
//printing of menu:
printf("Choose from the menu:\n");
printf(" [1] Add Student\n");
printf(" [2] Search Student\n");
printf(" [3] View All\n");
printf(" [4] Exit\n");
scanf("%d", &choice);
if((choice>=1)&&(choice<=4)){
if(choice==1){
printf("Enter student number:\n");
scanf("%d", &student[n].number);
printf("Enter the name of the student:\n");
scanf("%[^\n]", student[n].name);
printf("Enter month of birth:\n");
scanf("%d", &student[n].birthday.month);
printf("Enter day of birth:\n");
scanf("%d", &student[n].birthday.day);
printf("Enter year of birth:\n");
scanf("%d", &student[n].birthday.year);
printf("Enter course:\n");
scanf("%[^\n]", student[n].course);
n++;
}
if(choice==2){
while(i<n){
printf("%d\n", student[n].number);
printf("%s", student[n].name);
printf("%d/%d/%d", student[n].birthday.month, student[n].birthday.day,student[n].birthday.year);
printf("%s", student[n].course);
i++;
}
}
}
}
我刚刚完成了一半,因为我不知道如何寻找学生。希望您对我改进代码有任何建议。