2013-03-10 3 views
0

우리는 어떻게 구조체의 멤버를 검색하여 모든 멤버를 출력 할 수 있습니까?

  • 보기 학생을 추가 모든 학생들을

    • 에 사용자를 활성화 할 C의 특정 프로그램을 코딩했다 학생 종료

    에 대한

  • 검색 구조의 사용. 프로그램은 학생들의 포털처럼 행동해야합니다. 컴파일 할 때 Segmentation 오류 (코어 덤프) 오류를 출력하는 '임시 코드'가 있습니다. 이것이 내 코드가되는 방식입니다.

    #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++; 
          } 
         } 
         } 
    
    
        } 
    

    나는 학생을 어떻게 검색 할 것인지 잘 모르기 때문에 반쯤 지났습니다. 제 코드 개선을위한 제안이 있으시길 바랍니다.

  • +0

    segfault는 어디에서 발생합니까? 당신은 디버거에서 찾을 수 있습니까? – Floris

    답변

    0

    n을 초기화하는 것을 잊었습니다 (아마도 n = 0을 원할 것입니다).

    1

    가정하면, n 번째 요소에 도달 할 때까지 i을 사용하여 학생들을 반복합니다.

    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++; 
    } 
    

    과, n은 당신이 사용할 수있는 학생 secrh하기 위해 0

    0

    * 으로 초기화한다 이렇게하면 *

    int sno; 
    unsigned char flag=0; 
    printf("Enter student number to search :"); 
    scanf("%d",&sno); 
    


    이 레코드를 검색하여 모든 레코드로 검색하고 어떤 레코드와도 일치하면 해당 레코드를 표시합니다.

    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); } 
    
    관련 문제