2016-03-31 15 views
0

私はこの混乱の中でしばらく行ってきましたが、私はどこで間違っているのか分かりませんでした。ポインタのようなものであれば、構造体配列内の一致を検索する

表示されているタスク:学生ID、名前、生年月日、成績を含む構造配列を入力しようとしています。次に、ユーザーに与えられた一致IDで検索します。

この件に関するお手伝いをいただき、ありがとうございます。また、フランスの部品について事前にお詫び申し上げます。

// 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); 
    } 
+0

あなたが取得している出力は何ですか? Plz、あなたの変数と関数のより良い名前を使用します。 –

+2

'Dat'の要素は' int'ですが、あなたは '%d'ではなく'%s'でそれらを読み込んでいます。それは動作しません。 – Barmar

+0

@ViniciusZaramella申し訳ありませんが、これはフランス語のプロジェクトです。名前がフランス語になっている理由です。私が得ている出力は "Invalid Student!"です。私が働かなければならない検索をしようとするとき。それは二度書き込まれます。編集:私はそれを修正しました –

答えて

0

以下の編集コードは、あなたの目標を達成していると思います。主な問題は、構造体を関数に渡す方法でしたが、その値をFillStudentList関数の外で見ることができるように構造体を参照渡しする必要があります。 link

#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 
+0

返事ありがとうございますが、問題は解決しません。私はそれをやってみましたが、エラーが発生しました "エラーC2232: ' - > nom':左側のオペランドに 'struct'型があります。 " –

+0

投稿したコードを正確にコピーできますか?それは私のために働いています。入力とコード出力を含めるように答えを更新しました。 – PZwan

+0

@PZwanあなたのコードにはまだいくつかのエラーがあります。 'scanf("%s "、&E-> nom);' scanfは 'char *'を期待していますが、あなたは 'char(*)[20]'を使っています。同じことが 'scanf("%s "、&E-> prenom);' 'scanf("%s "、E-> nom);' 'とscanf("%s "、E-> prenom) ); ' – SSC

0

古典的な間違い:値ではなく、referenc通りかかっパラメータE:ここでは何が起こる

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

はあなたの構造のローカルコピーが入力された関数が終了したときに破壊されたものは何でも移入スタック、上で作成されていることです。

一般に、構造体を変更したくない場合でも、ポインタで構造体パラメータを渡します。値で渡すと、構造体の各メンバーがスタックにコピーされます...これは時間の無駄です。

だから、関数のプロトタイプ(と新しい署名で動作するようにコードが)問題を修正する必要があり変更:

void FillStudentList(struct Student *E){ 
.... 
} 
関連する問題