如何在C中搜索结构中的成员并打印所有成员?

如何在C中搜索结构中的成员并打印所有成员?,c,C,我们被要求用c语言编写一个特定的程序,使用户能够 添加一个学生 查看所有学生 搜索学生并退出 使用结构。该计划应该像一个学生的门户网站。 我有一个“暂定代码”,在编译时,会打印一个分段错误(内核转储)。我的代码是这样的: #include<stdio.h> typedef struct tag1{ int day, year, month; }Date; typedef struct tag2{ int number; char name[50];

我们被要求用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++;
            }
        }
        }


    }
#包括
typedef结构标记1{
整数天、年、月;
}日期;
typedef结构标记2{
整数;
字符名[50];
炭层[30];
生日;
}记录;
main(){
整数选择,n,i=0;
记录学生[200];
//菜单的打印:
printf(“从菜单中选择:\n”);
printf(“[1]添加学生\n”);
printf(“[2]搜索学生\n”);
printf(“[3]查看全部\n”);
printf(“[4]退出\n”);
scanf(“%d”,选择(&C);

如果((choice>=1)和(&)(choice您忘记初始化n(您可能想要n=0)。

假设,您使用
i
迭代学生,直到到达第n个元素

所以它应该是
student[i]
而不是
student[n]

这应该起作用:

//...

while(i<n){ 

      Record current = student[i]; 

      printf("%d\n", current.number);
      printf("%s", current.name);
      printf("%d/%d/%d", current.birthday.month, 
                   current.birthday.day,
                   current.birthday.year);
      printf("%s", current.course);

      i++;
}
/。。。
而对于一个学生,你可以这样使用*


然后将此记录搜索到所有记录,当它与任何记录匹配时,显示该记录。

for(i=0;i<n;i++) // where n is maximum number of records
{
     if(student[i].number == sno)
     {
         flag=1;
         /*** print all the member of student[i] ****/
         break;
     }
} // end of for loop
if(0==flag) { printf("\nSorry !!! Record not found with student number : %d\n",sno); }

for(i=0;i)segfault发生在哪里?您可以在调试器中运行以找出原因吗?
for(i=0;i<n;i++) // where n is maximum number of records
{
     if(student[i].number == sno)
     {
         flag=1;
         /*** print all the member of student[i] ****/
         break;
     }
} // end of for loop
if(0==flag) { printf("\nSorry !!! Record not found with student number : %d\n",sno); }