2016-03-31 66 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',但是你用'%s'而不是'%d'來讀取它們。這是行不通的。 – Barmar

+0

@ViniciusZaramella對不起,這是一個法國項目,所以這就是爲什麼這些名字是法國人。我得到的輸出是「無效的學生!」當我試圖做一個應該工作的搜索。它被寫入兩次。編輯:我修好了 –

回答

0

我相信下面的編輯代碼可以達到您的目標。主要問題(除了用'%s'讀取/打印'int')是將結構傳遞給函數的方式,必須通過引用來傳遞結構,以便可以在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':左操作數有'結構'類型,使用'。' 「 –

+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

典型的錯誤:按值,而不是通過和借鑑傳遞參數E:

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

這裏會發生什麼事是你結構的本地副本堆棧,填充任何輸入是創建和銷燬該函數退出時。

通常在C中,即使您不想修改結構,您也可以通過指針傳遞結構參數;如果按值傳遞它們,每個結構體的成員都會被複制到堆棧中......這是浪費時間的一個內存。

因此改變函數原型(並用新的簽名工作的代碼)應該可以解決這個問題:

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