Поиск совпадения в структурном массиве

Я был в этом беспорядке некоторое время, и я все еще не понял, где я иду не так, как надо, полностью ножом, если это что-то нелепое, как указатель.

Показанная задача: Попытка заполнить структурный массив идентификатором учащегося, именем, фамилией, датой рождения и оценками. Затем выполните поиск по сопоставленному идентификатору, предоставленному пользователю.

Я был бы очень признателен за любую помощь, связанную с этим предметом, я серьезно застрял на нем некоторое время. Также заранее прошу прощения за французские части

// Part 1
struct Date{
    int day;
    int month;
    int year;
};

// Part 2
struct Student{
    int ID;
    char name[20];
    char lastname[20];
    struct Date DOB;
    int notes[J];
};

// Part 3
void FillStudentList(struct Student E){
    int i;
    printf("\nInsert ID: ");
    scanf("%d", &E.ID);
    printf("Insert name: ");
    scanf("%s", &E.name);
    printf("Insert last name: ");
    scanf("%s", &E.lastname);
    printf("Insert date of birth: ");
    scanf("%d %d %d", &E.DOB.day, &E.DOB.month, &E.DOB.year);
    printf("Insert notes: ");
    for(i=0; i<J; i++)
        scanf("%d", &E.Notes[i]);
}

// Part 4
void ShowByNb(int Nb, struct Student E[], int NbStudents){
    int j, i;
    for(i=0; i<NbStudents; i++){
        if (E[i].ID== Nb){
            printf("\nID: %d", E[i].ID);
            printf("\nName: %s", E[i].name);
            printf("\nLast Name: %s", E[i].lastname);
            printf("\nDate Of Birth: %s-%s-%s", E[i].DOB.day, E[i].DOB.month, E[i].DOB.year);
            printf("\nNotes: ");
            for(j=0; j<J; j++){
                printf("%d", E[i].Notes[j]);
            }
        }
        else
            printf("\nInvalid Student!\n");
    }
}

// Part 5
void main(){
    int i, x;
    struct Student E[N];
    for(i=0; i<N; i++){
        printf("\n\nStudent #%d", i+1);
        FillStudentList(E[i]);
    }

    printf("\n\nSearch student by NB: ");
    scanf("%d", &x);
    ShowByNb(x, E, N);
    } 

2 ответа

Классическая ошибка: передача параметра по значению, а не по ссылке:

void FillStudentList(struct Student E){
.....
}

Здесь происходит то, что локальная копия вашей структуры создается в стеке, заполняется тем, что вводится, и уничтожается при выходе из функции.

Обычно в C, даже если вы не хотите изменять структуру, вы передаете параметры структуры по указателю; если вы передаете их по значению, каждый член структуры копируется в стек... что является пустой тратой времени памяти.

Таким образом, изменение прототипа функции (и кода для работы с новой подписью) должно решить проблему:

void FillStudentList(struct Student *E){
....
}

Я полагаю, что отредактированный код ниже достигает вашей цели. Основная проблема (кроме чтения / печати int'ов с помощью '%s') заключалась в том, как вы передаете свою структуру своим функциям. Необходимо передать структуру по ссылке, чтобы ее значения можно было увидеть за пределами функции FillStudentList; см. Это ссылка

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define N 2
#define J 2

// Part 1
struct Dat{
    int jour;
    int mois;
    int annee;
};

// Part 2
struct Etudiant{
    int numero;
    char nom[20];
    char prenom[20];
    struct Dat DDN;
    int Notes[J];
};

// Part 3
/* Modified this so that a pointer to the struct is passed instead of a copy of the struct */
void FillStudentList(struct Etudiant *E){
    int i;
    printf("\nInsert ID: ");
    scanf("%d", &E->numero);
    printf("Insert name: ");
    scanf("%s", E->nom);
    printf("Insert last name: ");
    scanf("%s", E->prenom);
    printf("Insert date of birth: ");
    /* These are integers. Do not read with %s */
    scanf("%d %d %d", &E->DDN.jour, &E->DDN.mois, &E->DDN.annee);
    printf("Insert notes: ");
    for(i=0; i<J; i++)
        scanf("%d", &E->Notes[i]);
}

// Part 4
void ShowByNb(int Nb, struct Etudiant E[]){
    /* Don't redefine N == NbEtudiants making it seem that N is variable */
    int j, i;
    for(i=0; i<N; i++){
        if (E[i].numero == Nb){
            printf("\nID: %d", E[i].numero);
            printf("\nName: %s", E[i].nom);
            printf("\nLast Name: %s", E[i].prenom);
            /* Again, can't print integers with %s */
            printf("\nDate Of Birth: %d-%d-%d", E[i].DDN.jour, E[i].DDN.mois, E[i].DDN.annee);
            printf("\nLes notes: ");
            for(j=0; j<J; j++){
                printf("%d ", E[i].Notes[j]);
            }
            return;
        }
        /* Your previous else would print invalid student every time you ran through the loop even 
         * if the student number was valid for a later student.
         */
    }
    /* Only print this if student was not found in any of the N Student structures */
    printf("\nInvalid Student!\n");
}

// Part 5
void main(){

    setbuf(stdout, NULL);

    int i, x;
    struct Etudiant E[N];

    for(i=0; i<N; i++){
        printf("\n\nStudent #%d", i+1);
        FillStudentList(&E[i]);
    }

    printf("\n\nSearch student by NB: ");
    scanf("%d", &x);
    ShowByNb(x, E);
}

вход

1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2

Выход

Student #1
Insert ID: 1
Insert name: 1
Insert last name: 1
Insert date of birth: 1
1
1
Insert notes: 1
1


Student #2
Insert ID: 2
Insert name: 2
Insert last name: 2
Insert date of birth: 2
2
2
Insert notes: 2
2


Search student by NB: 2

ID: 2
Name: 2
Last Name: 2
Date Of Birth: 2-2-2
Les notes: 2 2
Другие вопросы по тегам