Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[20];
int wins;
int loses;
double pct;
}TEAM;
void gameResult(TEAM* home, TEAM* away, int homeScore, int awayScore);
void calculatePCT(TEAM* team);
void leagueStandings(TEAM teams[]);
int getIndex(TEAM teams[], char* teamName);
int main()
{
//declare variables
TEAM teams[6];
int input=0;
int homeScore, awayScore, homeIndex, awayIndex;
char home[20];
char away[20];
int i;
for (i=0;i<6;i++){
memset(teams[i].name,'\0',20);
}
memset(home,'\0',20);
memset(away,'\0',20);
sprintf(teams[0].name,"Houston");
teams[0].wins=0;
teams[0].loses=0;
sprintf(teams[1].name,"Milwaukee");
teams[1].wins=0;
teams[1].loses=0;
sprintf(teams[2].name,"StLouis");
teams[2].wins=0;
teams[2].loses=0;
sprintf( teams[3].name,"Cincinatti");
teams[3].wins=0;
teams[3].loses=0;
sprintf( teams[4].name,"Pittsburgh");
teams[4].wins=0;
teams[4].loses=0;
sprintf( teams[5].name,"Chicago");
teams[5].wins=0;
teams[5].loses=0;
while(input != 3){
printf("Sports League Menu\n");
printf("1) Enter the result of the game\n");
printf("2) Display the league standings\n");
printf ("3) Quit the program\n");
scanf ("%d", &input);
switch(input){
case 1:
fflush(stdin);
printf("Enter the home team's name:\n");
printf("(Houston, Milwaukee, StLouis, Cincinnati, Pittsburgh, or Chicago\n\n");
scanf ("%20s", &home);
printf("Enter the away team's name:\n");
scanf ("%20s", &away);
printf("Enter the home score:\n");
scanf ("%i", &homeScore);
printf("Enter the away score:");
scanf ("%i", &awayScore);
homeIndex = getIndex(teams, home);
awayIndex = getIndex(teams, away);
if (homeIndex != -1 && awayIndex != -1)
gameResult(&teams[homeIndex], &teams[awayIndex],homeScore, awayScore);
else printf("invalid");
break;
case 2:
leagueStandings(teams);
break;
case 3:
printf("\nGoodbye");
break;
}//end switch
}//end while
system ("pause");
return 0;
}
void gameResult(TEAM* home, TEAM* away, int homeScore, int awayScore){
if (homeScore < awayScore){
away->wins++;
home->loses++;
}
else {
home->wins++;
away->loses++;
}
}
void calculatePCT(TEAM* team){
int total=(team->wins + team->loses);
if (total > 0)
team->pct = team->wins / total;
else
team->pct = 0;
}
int getIndex(TEAM teams[], char* teamName) {
int result = -1;
int i;
for (i = 0; i < 6 && result == -1; i++) {
if (strcmp(teams[i].name, teamName) == 0) {
result = i;
}
}
return result;
}
void leagueStandings(TEAM teams[])
{
int i;
printf("Team Name\t\tWins\t\tLoses\t\tPCT\n");
for (i=0;i<6;i++){
calculatePCT(&teams[i]);
printf("%s20%i\t\t%i\t\t%.3lf\n",teams[i].name, teams[i].wins,teams[i].loses,teams[i].pct);
}
}