Hello ive created a football league program that does everything but allow me to update my table with scores, please have a look bellow.
Code:
#include <stdio.h>#include <string.h>
#define numberofteams 3
#define numberofgames (numberofteams * (numberofteams - 1))
struct team
{
char name[20];
int points;
int gamesplayed;
int gamepoints;
int played;
int win;
int draw;
int lose;
};
struct game
{
char hometeam[20];
char awayteam[20];
int homegoals;
int awaygoals;
};
struct leagueTable
{
int noOfTeams;
struct team lgTbl[numberofteams];
};
void cleargames(struct game games[]);
void enterteamnames(struct team league[]);
void enterresults(struct team league[], struct game games[]);
void displayAllResults(struct team league[numberofteams]);
int main(void)
{
struct team league[numberofteams];
struct game games[numberofgames];
int choice;
char stringchoice[3];
cleargames(games);
do {
printf("Welcome to your fantasy football league \n\n"
"1) Enter The Team Names \n"
"2) Enter The Results of Teams - Not Finished \n"
"3) Show The Whole League Table - displays all names of teams \n"
"4) Option Four \n"
"5) Exit \n");
gets(stringchoice);
choice = atoi(stringchoice);
/* atoi converts string to integer*/
if (choice == 1)
{
enterteamnames(league);
}
else if (choice == 2) {
enterresults(league, games);
}
else if (choice == 3) {
displayAllResults(league);
}
else if (choice == 4) {}
else if (choice == 5) {}
else {
printf("Invalid Choice\n\n");
}
} while (choice != 5);
return 0;
}
void cleargames(struct game games[])
{
int i;
for(i = 0; i < numberofgames - 1; i++)
{
strcpy(games[i].hometeam, "");
strcpy(games[i].awayteam, "");
games[i].homegoals = 0;
games[i].awaygoals = 0;
}
}
void enterteamnames(struct team league[])
{
int i;
for(i = 0; i < numberofteams; i++)
{
printf("Team name: ");
gets(league[i].name);
league[i].played = 0;
league[i].win = 0;
league[i].draw = 0;
league[i].lose = 0;
league[i].points = 0;
}
}
void enterresults(struct team league[], struct game games[])
{
int i;
int gamenumber = 0;
int homenum;
int awaynum;
int d;
for(i=0;i < numberofteams; i++)
{
if(strcmp(games[i].hometeam, "") == 0)
{
gamenumber = i;
break;
}
}
printf("Enter The Home Team Name: ");
gets(games[gamenumber].hometeam);
for(i=0;i < numberofteams; i++)
if(strcmp(games[gamenumber].hometeam, league[i].name) == 0)
homenum = i;
for(i=0;i < numberofteams; i++)
if(strcmp(games[gamenumber].awayteam, league[i].name) == 0)
awaynum = i;
printf("Enter The Away Team Name: ");
gets(games[gamenumber].awayteam);
printf("Enter The Score of the game ");
scanf("%d - %d",&games[gamenumber].homegoals, &games[gamenumber].awaygoals);
fflush(stdin);
printf("\n%d %d", games[gamenumber].homegoals, games[gamenumber].awaygoals);
if(games[gamenumber].homegoals > games[gamenumber].awaygoals)
{
league[homenum].win++;
league[awaynum].lose++;
league[homenum].points+=2;
league[awaynum].points-=1;
}
else if(games[gamenumber].homegoals < games[gamenumber].awaygoals)
{
league[homenum].lose++;
league[awaynum].win++;
league[homenum].points-=1;
league[awaynum].points+=2;
}
else if(games[gamenumber].homegoals == games[gamenumber].awaygoals)
{
league[homenum].draw++;
league[awaynum].draw++;
league[homenum].points++ ;
league[awaynum].points++ ;
}
}
void displayAllResults(struct team league[numberofteams])
{
int d;
printf("\n\nPremier League Table\n");
printf("\n---------------------------------------------------\n");
printf("\t Name P W D L Pts\n");
printf("-----------------------------------------------------\n");
for (d=0;d<numberofteams;d++)
{
printf("%15s %3d %3d %3d %3d %3d\n", league[d].name,league[d].played, league[d].win, league[d].draw,league[d].lose,
league[d].points);
}
}