我们被要求用C编写一定的方案,使用户能够如何在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++;
}
}
}
}
我只是中途因为我有我将如何为学生寻找不知道。希望你对我有任何改进我的代码的建议。
段错误发生在哪里?你可以运行调试器来找出答案吗? – Floris 2013-03-10 04:50:41