I have to create function that reads the binary file of futball matches and shows the legue table. The table should contains, for each team, the name of the team, number of points, goals scored and goals conceded. Give 3 points for a win and 1 for a draw.
The format of the binary file is:
Inter Milan,Real Madrid,2,1
Real Madrid,Juventus,3,1
Juventus,Barcelona,1,0
Barcelona,Chelsea,0,5
Chelsea,Manchester Utd,5,2
...
I got the next code, but it comparing only first column of strings and ignoring second column. I can't find the error.
Can you help me please.
/*Implement league_table()*/
void league_table(FILE *binary,struct match single_match,team_no team_array[])
{
struct match buffer[SIZE];
int read = 0;
int count=0;
int i,j;
int check;
int numA,numB;
int scored[10]={0};
int conc[10] = {0};
int points[10] = {0};
printf("\n League Table \n");
printf("________________________________________________________________________________\n");
printf("--------------------------------------------------------------------------------\n");
i = 0;
//read the binary file and copy teams name to the team struct
if((binary = fopen("matches.bin","rb"))!= NULL)
{
while(feof(binary) == 0 && i<180)
{
read = fread(&single_match,sizeof(struct match),1,binary);
if(read == 0)
{
printf("\n Error in reading matches.bin");
}
else
{
//printf(" %s %s %d %d",single_match.team_A, single_match.team_B, single_match.goals_A, single_match.goals_B);
strcpy(buffer[i].team_A, single_match.team_A);
strcpy(buffer[i].team_B, single_match.team_B);
buffer[i].goals_A = single_match. goals_A;
buffer[i].goals_B = single_match. goals_B;
i++;
}
}
}
else
{
printf("\n Error in opening binary file to read!!!");
exit(1);
}
fclose(binary);
i = 0;
//copy teams names to the struct team
for(i = 0; i<10; i++)
{
strcpy(team_array[i].name,buffer[i].team_A);
team_array[i].number = i + 1;
// printf(" %s %d ",team_array[i].name, team_array[i].number);
}
// count points ,scored goals and conceded goals
for(i = 0; i < 10; i++)
{
for(j = 0; j<SIZE; j++)
{
check = strcmp(team_array[i].name,buffer[j].team_A);
if(check == 0)
{
numA = buffer[j].goals_A;
scored[i] = scored[i] + numA;
numB = buffer[j].goals_B;
conc[i] = conc[i] + numB;
count++;
if(buffer[j].goals_A > buffer[j].goals_B)
{
points[i] = points[i] + 3;
}
if(buffer[j].goals_A == buffer[j].goals_B)
{
points[i] = points[i] + 1;
}
else
{
points[i] = points[i] + 0;
}
//printf("\n %s %s %d %d ",buffer[j].team_A, buffer[j].team_B, buffer[j].goals_A, buffer[j].goals_B);
}
else
{
check = strcmp(team_array[i].name,buffer[j].team_B);
if(check == 0)
{
numB = buffer[j].goals_B;
scored[i] = scored[i] + numB;
numA = buffer[j].goals_A;
conc[i] = conc[i] + numA;
count++;
if(buffer[j].goals_A < buffer[j].goals_B)
{
points[i] = points[i] + 3;
}
if(buffer[j].goals_A == buffer[j].goals_B)
{
points[i] = points[i] + 1;
}
printf("\n %s %s %d %d ",buffer[j].team_A, buffer[j].team_B, buffer[j].goals_A, buffer[j].goals_B);
}
}
}
printf(" %d |", count);
printf(" %d", scored[i]);
printf(" %d", conc[i]);
printf(" %d\n\n",points[i]);
count = 0;
}
i = 0;
for(i = 0; i<10; i++)
{
team_array[i].scored = scored[i];
team_array[i].conceded = conc[i];
team_array[i].points = points[i];
//printf(" %d",team_array[i].number);
printf("%s ",team_array[i].name);
printf("\t%i ",team_array[i].scored);
printf("\t%i ",team_array[i].conceded);
printf("\t%i \n",team_array[i].points);
}
}//end league_table()