2014-09-04 55 views
0

我有以下結構更改結構陣列(從文件中讀取)

struct candidates 
    { 
    char name[20]; 
    int votes; 
    }; 
struct candidates electionCandidates[]; 

我需要在從文件至7

我可以讀取和更新的名稱electionCandidates [0]用下面的方法這樣做

for (i = 0; i < 7; i++) 
    { 
     fgets(electionCandidates[i].name, 20, (FILE*)fp); 
    } 

但我需要在函數中做到這一點。

我已經試過

void Initialize(struct candidates* EC[]); 

Initialize(&electionCandidates); 

void Initialize(struct candidates* EC[]) 
{ 
    int i; 

    FILE *fp; 
    fp = fopen("elections.txt", "r"); 

    for (i = 0; i < 7; i++) 
    { 
     fgets(EC[i].name, 20, (FILE*)fp); 
    } 

    fclose(fp); 
} 

口口聲聲說不會看到名稱。或者整個事情可能是錯的。我不確定。

任何幫助,將不勝感激。

+1

'結構候選electionCandidates [7];','空隙初始化(結構候選EC []);','初始化(electionCandidates);' – BLUEPIXY 2014-09-04 19:00:14

+0

的'electionCandidates'聲明在文件範圍具有空的方括號是可疑;你需要指定它的大小。您應該檢查文件是否成功打開。你應該總是檢查'fgets()'的結果來檢測早期的EOF。您將'fp'轉換爲'(FILE *)fp'的調用語法是多餘的,表示缺乏置信度。傳遞'&electionCandidates'傳遞一個指向數組的指針('struct candidates(*)[]'),而不僅僅是一個指針,就像你丟掉'&'一樣。 – 2014-09-04 19:01:17

回答

0

鑑於

struct candidates 
    { 
    char name[20]; 
    int votes; 
    }; 
struct candidates electionCandidates[]; 

你想

void Initialize(struct candidates EC[]) 
{ 
    /* ... */ 
} 

Initialize(electionCandidates); 

當作爲函數參數傳遞,結構的陣列衰減到一個指向它的第一個元件。

+0

這是工作:D我正在獲取名稱現在加載。謝謝大家 – Sclaar 2014-09-04 19:23:56

+0

小心!從** BLUEPIXY **和** Johathan Lefler **再次閱讀您的問題的評論。具體來說,聲明'electionCandidates [];'的可疑性質 – 2014-09-04 20:47:35

1

您需要查看Initialize()函數簽名,並打開編譯器警告(如果它們已經打開,請注意它們)。您正在聲明Initialize()以獲取指向struct candidates的指針數組,這不是您正在傳遞的數組 - struct candidates的數組。數組衰減爲一個指針,所以struct candidates *EC是你的論點應該看起來像(或者,struct candidates EC[],在這種情況下相當於),而不是struct candidates *EC[]。然後調用你的函數沒有& ...

0

我得到它加載名稱和一切,但我想我需要指向這個SPOIL?它不是閱讀或者進行投票/被棄的選票,兩者都是0。

#include <stdio.h> 

struct candidates 
{ 
char name[20]; 
int votes; 
}; 
struct candidates electionCandidates[7]; 

void Initialize(struct candidates EC[]); 
void Processvotes(struct candidates EC[], int BadVote); 
void printResults(struct candidates EC[], int BadVote); 

int main() 

{ 

    int i, SPOIL = 0; 

    Initialize(electionCandidates); 
    Processvotes(electionCandidates, SPOIL); 
    printResults(electionCandidates, SPOIL); 

} 

void Initialize(struct candidates EC[]) 
{ 
    int i; 

    FILE *fp; 
    fp = fopen("elections.txt", "r"); 

    for (i = 0; i < 7; i++) 

    { 
     fgets(EC[i].name, 20, (FILE*)fp); 
    } 

    fclose(fp); 
} 


void Processvotes(struct candidates EC[], int BadVote) 
{ 
     int TVOTE, i; 

     FILE *fp; 
     fp = fopen("elections.txt", "r"); 

     fscanf(fp, "%d", &TVOTE); 

     for (i = 0; i < 365; i++) 
     { 
      if (TVOTE == 1) 
       EC[0].votes++; 
      if (TVOTE == 2) 
       EC[1].votes++; 
      if (TVOTE == 3) 
       EC[2].votes++; 
      if (TVOTE == 4) 
       EC[3].votes++; 
      if (TVOTE == 5) 
       EC[4].votes++; 
      if (TVOTE == 6) 
       EC[5].votes++; 
      if (TVOTE == 7) 
       EC[6].votes++; 

      if (TVOTE < 1 || TVOTE > 7) 
       BadVote++; 
     } 

     fclose(fp); 
} 

void printResults(struct candidates EC[], int BadVote) 
{ 

    int i, Win = 0, WinSCORE = 0, Runner = 0, RunnerSCORE = 0; 

    for (i = 0; i < 7; i++) 
    { 

     if (EC[i].votes > WinSCORE) 
     { 
      WinSCORE = EC[i].votes; 
      Win = i; 
     } 

     if (EC[i].votes == WinSCORE) 
     { 
      RunnerSCORE = EC[i].votes; 
      Runner = i; 
     } 
    } 


    if (WinSCORE == RunnerSCORE) 
    { 
     printf("There was a tie between %s and %s who both got a total of %d votes each. There were %d spoiled votes\n", EC[Win].name, EC[Runner].name, WinSCORE, BadVote); 
    } 

    else 
     printf("%s won the election with a total of %d votes. There was a total of %d spoiled votes.\n", EC[Win].name, WinSCORE, BadVote); 


}