2017-07-31 78 views
-5

我正在寫一個計算足球比賽結果的程序。我試圖將隊ids'存儲在頂部定義長度爲10的數組中,但是我一直從數組中獲取構建錯誤。我意識到語法可能是錯誤的,但我還可以如何使用變量來指定數組長度? 我收到的錯誤消息是:'{'標記之前的預期表達式。我如何將數組存儲在C中?

#include <stdio.h> 
#include <stdlib.h> 
#define ARRAY_SIZE 10 

int main() { 
    int numberofmatches, hometeamid, awayteamid, hometeamgoals, awayteamgoals; 
    int hometeamwins = 0; 
    int winratio; 
    int teamid[ARRAY_SIZE]; 
    printf("Enter number of matches played \n"); 
    scanf("%d", &numberofmatches); 

    if (numberofmatches > 0) { 

     int x = 0; 
     do { 

      printf("Enter match stats in order Home_team_ID,Away_Team_ID,Goals_Home,Goals_Away\n"); 
      scanf("%d %d %d %d", &hometeamid, &awayteamid, &hometeamgoals, &awayteamgoals); 

      teamid[ARRAY_SIZE] = {hometeamid}; //Error is on this line 

      if (hometeamgoals > awayteamgoals) { 
       hometeamwins++; 
      } 
      x++; 
     } 
     while (x < numberofmatches); 
     winratio = hometeamwins/numberofmatches; 
     printf(" %d :teamidth %d :winratio", teamid[0], winratio); 

    } 
    return 0; 

} 
+0

您是否嘗試閱讀錯誤? – SLaks

+0

錯誤說的是什麼?它是數組還是你的while循環? –

+2

你覺得'teamid [ARRAY_SIZE] = {hometeamid};'是在你的循環中進行的嗎? – Yunnosch

回答

0

你的限定尺寸10的陣列,然後在索引10訪問它由於陣列是索引0..N-1需要訪問索引9,以獲得陣列而不是10的端部。

+1

這不會導致構建錯誤。 – Yunnosch

+1

這是一個運行時錯誤 –

+0

抱歉沒讀過問題的一部分,直到錯誤消息更新,這是我可以看到的唯一錯誤。 – silassales

1


teamid[ARRAY_SIZE] = {hometeamid};
是用於限定尺寸ARRAYSIZE的陣列,並且不完全初始化它的語法。
您可以在循環中嘗試它。

如果你想要寫一個數組成員,你可能想 teamid[x] = hometeamid;

另外,我建議確保你不寫超出teamid [9],這是數組的最後一個合法成員,對於ARRAY_SIZE == 10.