#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 20
//struct team_info
typedef struct
{
char team[16];
int played;
int won;
int drawn;
int lost;
int goalsf;
int goalsa;
int goald;
int points;
int ID;
}team_info;
team_info arrdetails[12];
int print_header();
void draw_table ();
int take_team_input();
int take_team_name();
int save_table();
int read_table();
int sort_table();
int num_teams = 0;
/************************************************************/
/* Main Function */
/************************************************************/
int main()
{
int option = 0;
while(option != 7)
{
option = print_header();
switch(option)
{
case 1: /*Enter team */
if (num_teams < MAX)
{
take_team_name();
break;
}
else
{
printf("LEAGUE FULL\n");
}
case 2: /*Display table*/
printf("\nLeague Table:\n");
draw_table();
break;
case 3: /*Match details*/
take_team_input();
break;
case 4: /*Save File*/
save_table();
break;
case 5: /*Readfile*/
read_table();
break;
case 6: /*Sort Table*/
sort_table();
break;
case 7: /*Exit*/
printf("\n*** FINISHED ***\n");
exit(0);
default: /*invalid option letter entererd*/
printf("\nInvalid choice, Please try again\n");
}
}
return 0;
}
/****************************************************************/
/* Functions */
/****************************************************************/
/****************************************************************/
/* Print Menu Options */
/****************************************************************/
int print_header()
{
int a=0;
printf("\n****************************");
printf("\n* Football League *");
printf("\n****************************\n");
printf("\n1.\tEnter a New Team\n");
printf("\n2.\tDisplay Current Table\n");
printf("\n3.\tEnter Match Details\n");
printf("\n4.\tSave to Filels\n");
printf("\n5.\tRead from File File\n");
printf("\n6.\tSort Table By Points\n");
printf("\n7.\tExit System\n");
printf("\nPlease Enter Your Choice: \n");
scanf("%d", &a);
return a;
}
/****************************************************************/
/* (1) Enters Team Name */
/****************************************************************/
int take_team_name()
{
char teamd[16];
printf("\nName:\n>");
scanf("%s", teamd);
strcpy(arrdetails[num_teams].team, teamd);
arrdetails[num_teams].played = 0;
arrdetails[num_teams].won = 0;
arrdetails[num_teams].drawn = 0;
arrdetails[num_teams].lost = 0;
arrdetails[num_teams].goalsf = 0;
arrdetails[num_teams].goalsa = 0;
arrdetails[num_teams].goald = 0;
arrdetails[num_teams].points = 0;
arrdetails[num_teams].ID = num_teams;
num_teams++;
return 0;
}
/****************************************************************/
/* (2)Enter Match Details */
/****************************************************************/
int take_team_input()
{
char team[16];
int ID = 0;
int player1 = 0;
int player2 = 0;
int score1 = 0;
int score2 = 0;
int i;
printf("\n%2s %-16s\n","ID","Team Name");
//takes home and away team ID and score
for (i = 0; i < num_teams; ++i)
{
strcpy(team, arrdetails[i].team);
ID = arrdetails[i].ID;
printf("%2d %-16s\n", ID, team);
}
printf("\nSelect Home Team\n>");
scanf("%d", &player1);
printf("\nEnter their Score\n>");
scanf("%d", &score1);
printf("\nSelect Away Team\n>");
scanf("%d", &player2);
printf("\nEnter their Score\n>");
scanf("%d", &score2);
// works out played etc if draw
if (score1 == score2)
{
arrdetails[player1].played++;
arrdetails[player2].played++;
arrdetails[player1].goalsf = arrdetails[player1].goalsf + score1;
arrdetails[player2].goalsf = arrdetails[player2].goalsf + score2;
arrdetails[player1].goalsa = arrdetails[player1].goalsa + score2;
arrdetails[player2].goalsa = arrdetails[player2].goalsa + score1;
arrdetails[player1].points++;
arrdetails[player2].points++;
arrdetails[player1].drawn++;
arrdetails[player2].drawn++;
}
// works out played etc if home team wins
if (score1 > score2)
{
arrdetails[player1].played++;
arrdetails[player2].played++;
arrdetails[player1].goalsf = arrdetails[player1].goalsf + score1;
arrdetails[player2].goalsf = arrdetails[player2].goalsf + score2;
arrdetails[player1].goalsa = arrdetails[player1].goalsa + score2;
arrdetails[player2].goalsa = arrdetails[player2].goalsa + score1;
arrdetails[player1].points += 3;
arrdetails[player1].won++;
arrdetails[player2].lost++;
}
// works out played etc if away team wins
if (score1 < score2)
{
arrdetails[player1].played++;
arrdetails[player2].played++;
arrdetails[player1].goalsf = arrdetails[player1].goalsf + score1;
arrdetails[player2].goalsf = arrdetails[player2].goalsf + score2;
arrdetails[player1].goalsa = arrdetails[player1].goalsa + score2;
arrdetails[player2].goalsa = arrdetails[player2].goalsa + score1;
arrdetails[player2].points += 3;
arrdetails[player1].lost++;
arrdetails[player2].won++;
}
return 0;
}
/****************************************************************/
/* (3) Displays the current teams */
/* And Info */
/****************************************************************/
void draw_table ()
{
int i;
// Prints out Headers
printf("Id Team Played Won Lost Drawn GoalsF GoalsA GoalD Points\n");
// Displays Sorted League Table
for ( i = 0; i < MAX; i++ )
{
printf("%d \t %10s \t %d \t %d \t %d \t \t %d\n",arrdetails[i].ID,arrdetails [i].team,arrdetails[i].played,arrdetails[i].won,arrdetails[i].lost,arrdetails[i].drawn,arrdetails[i].goalsf,arrdetails[i].goalsa,arrdetails[i].goald,arrdetails[i].points);
}
}
/****************************************************************/
/* (4) Save To A File */
/****************************************************************/
int save_table ()
{
int i;
FILE *stream;
if(stream = NULL)
{
printf("Unable to open\n");
return -1;
}
else
{
printf("Save: ");
}
for(i=0; i< 12+1; i++)
{
fwrite(&arrdetails[i], sizeof(team_info), 1, stream);
printf("#");
}
fclose(stream);
printf("\n");
return 0;
}
/****************************************************************/
/* (4) Read From A File */
/****************************************************************/
int read_table ()
{
int i = 0;
FILE *stream;
if(stream = NULL)
{
return -1;
printf("Failed to open file");
}
else
{
printf("Read: ");
}
while(fread(&arrdetails[i] sizeof(team_info), 1, stream))
{
i++;
printf("#");
}
printf("\n");
fclose(stream);
return i;
}
/****************************************************************/
/* (6) Sort League Table */
/****************************************************************/
int sort_table ()
{
//FILL IN HERE LATER
}
cannot get the file to save and read keep getting segmentation fault (core dumped) errors
its probs somthing very simple as im not too good at c, everything worked before i added the bits to read and write
hope someone can help
regards Dan