我正在寫一個計算足球比賽結果的程序。我試圖將隊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;
}
您是否嘗試閱讀錯誤? – SLaks
錯誤說的是什麼?它是數組還是你的while循環? –
你覺得'teamid [ARRAY_SIZE] = {hometeamid};'是在你的循環中進行的嗎? – Yunnosch