Code:
#include <stdio.h>
typedef struct {
char name[12];
int p,w,d,l,f,a,t;
} team;
char showmenu();
int teamentry();
int showtable(int);
void inputmatchresults(int);
team side[12];
int main(void)
{
char c, returnc;
int n = -1;
while ((c = showmenu()) != 'd') {
switch(c) {
case 'a':
printf("enter teams\n");
n = teamentry();
break;
case 'b':
if (n == -1) {
printf("You need to enter the teams first\n");
} else {
printf("display the current table\n");
showtable(n);
}
break;
case 'c':
if (n == -1) {
printf("You need to enter the teams first\n");
} else {
printf("enter match results\n");
inputmatchresults(n);
}
break;
default:
printf("make sure you have typed a valid option\n");
break;
}
}
printf("Exit\n");
return 0;
}
char showmenu()
{
char c, returnc;
printf("\nPlease choose an option:\n");
printf("\t a - input teams, b - show table, c - input results\n\t d - Exit$
printf("enter the option: ");
scanf("%c%c", &c, &returnc);
return c;
}
int teamentry()
{
char returnc;
int n,i;
printf("number of teams: ");
scanf("%d%c", &n,&returnc);
for (i =0; i<n; i++) {
printf("Team%d\n",(i+1));
printf("\tName: ");
scanf("%s%c", &side[i].name, &returnc);
side[i].p = 0;
side[i].w = 0;
side[i].d = 0;
side[i].l = 0;
side[i].f = 0;
side[i].a = 0;
side[i].t = 0;
}
return n;
}
int showtable(int n)
{
int i;
printf("%-12s %3s %3s %3s %3s %3s %3s %3s \n","","P","W","D","L","F","A","T$
for (i = 0; i<n; i++) {
printf("%-12s %3d %3d %3d %3d %3d %3d %3d\n",side[i].name, side[i].p,si$
}
return 0;
}
void inputmatchresults(int n)
{
int t1,t2,f1,f2;
char returnc;
reinput: //this uses a goto lable for the re-input
printf("the teams you wish to enter the results for are: ");
scanf("%d%d%c", &t1, &t2, &returnc);
//correct t1 and t2 for array
t1--; t2--;
if (t1 > n || t2 > n || t1 == t2 || t1 < 0 || t2 < 0) {
printf("Incorrect, please re-input teams\n");
goto reinput;
} else {
printf("goals scored for %s:", side[t1].name);
scanf("%d%c", &f1, &returnc);
printf("goals scored for %s:", side[t2].name);
scanf("%d%c", &f2, &returnc);
//check result
if (f1 > f2) {
//team1 wins
printf("%s wins\n", side[t1].name);
//update team1 winner
side[t1].p++;
side[t1].a += f2;
side[t1].f += f1;
side[t1].w++;
side[t1].t += 3;
//update team2 loser
side[t2].p++;
side[t2].a += f1;
side[t2].f += f2;
side[t2].l++;
} else if (f2 > f1) {
//team2 wins
printf("%s wins\n", side[t2].name);
//update team2 winner
side[t2].p++;
side[t2].a += f1;
side[t2].f += f2;
side[t2].w++;
side[t2].t += 3;
//update team1 loser
side[t1].p++;
side[t1].a += f2;
side[t1].f += f1;
side[t1].l++;
} else {
//draw
printf("Draw\n");
//update team1 draw
side[t1].p++;
side[t1].a += f2;
side[t1].f += f1;
side[t1].d++;
side[t1].t += 1;
//update team2 draw
side[t2].p++;
side[t2].a += f1;
side[t2].f += f2;
side[t2].d++;
side[t2].t += 1;
}
}
}