2017-07-31 75 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.