H
hpy_awad
I wrote that example from a book and there is en error in the display
module that it does not showing all the records are entered in the
input module.
I traced with some printf statments without getting the solution . I
think the error in the display module loop condition.
Thanks
#include <stdio.h>
//-exercise 7.5.1 - Goal Statistics
struct football
{
char name[30];
char team[30];
int goals;
};
main ()
{
struct football player[100];
int option;
//initialize player array
init_player(player);
do
{
//Display menu of options ;
menu();
//Determine users requirements
menu_choice(&option);
//Perform users specified option
switch(option)
{
case 1: //Enter information for all players
player_input(player);
break;
case 2: //display table for all players
goal_table(player);
break;
case 3: //update specific information for a player
goal_update(player);
break;
case 4: //exit from program
break;
default://illegal input
printf ("\n\n This is not an available option\nAvailable options are
1,2,3,4");
}
} while (option !=4);
}
init_player(player)
//------------------
struct football player[];
{
int i,j;
for (i=0;i<100;++i)
{
for (j=0;j<30;++j)
{
player.name[j]=' ';
player.team[j]=' ';
}
player.goals=0;
}
}
menu()
//------------------
{
system ("clear");
printf("\n\n football system ");
printf("\n ------------------- ");
printf("\n\n 1- Enter information ");
printf("\n\n 2- Display table for all players");
printf("\n\n 3- Update specific information");
printf("\n\n 4- Exit from program ");
}
menu_choice(opt)
//------------------
int *opt;
{
printf("\n\n Select one of the above (1-4) : ");
scanf("%d",opt);
//clear screen
system("clear");
}
player_input(player)
//------------------
struct football player[];
{
int i;
char more;
i=-1;
do
{
++i;
//input players name
printf("Enter players name : ");
input_string(player.name);
//input players team
printf("Enter players team : ");
input_string(player.team);
//input number of goals scored
fflush(stdin);
printf("Enter number of goals scored: ");
scanf("%d",&player.goals);
printf("\n %-30s %-30s %4d\n",player.name,player.team,player.goals);
printf("\n %d I Value inside insert\n ",i);
//more players
if (i<99)
{
printf("More playersd to be entered (y/n)---->");
scanf("%s",&more);
}
} while (more == 'y' && i<99);
//Terminate players list
if (i<99) player[i+1].goals=-1;
}
input_string(alpha)
//------------------
char *alpha;
{
int i;
i=-1;
//Flush the keyboard buffer
fflush(stdin);
do
{
++i;
//input a character
alpha=getchar();
} while (alpha !='\n' && i<29);
//Terminate string
alpha='\0';
}
goal_table(player) //Display table of goals scored
//------------------
struct football player[];
{
int i=0;
char cont;
//Output table headings
printf("\n\n Name Team Goals");
printf("\n\n ---- ---- -----");
do
{
//Output player information
printf("\n %d I Value inside display ",i);
printf("\n %-30s %-30s %4d\n",player.name,player.team,player.goals);
++i;
} while (i<=2);
printf("\n\n Press C to continue ");
scanf("%s",&cont);
}
goal_update(player) //update table of goals scored
//------------------
struct football player[];
{
char name[30];
int i,match,goal;
//input players name to be updated
printf("Enter name of player");
input_string(name);
//Find players record
i=0;
while ((player.goals!=-1)&&(i<100)&&(match=strcmp(name,player.name)
!=0))
++i;
//Input number of goals to be added
printf("Enter number of goals to be added to players account");
scanf("%d",&goal);
//Update players record
if (match==0)
player.goals=player.goals+goal;
else
printf("\n\n Player %s is not in the goal table \n",name);
}
module that it does not showing all the records are entered in the
input module.
I traced with some printf statments without getting the solution . I
think the error in the display module loop condition.
Thanks
#include <stdio.h>
//-exercise 7.5.1 - Goal Statistics
struct football
{
char name[30];
char team[30];
int goals;
};
main ()
{
struct football player[100];
int option;
//initialize player array
init_player(player);
do
{
//Display menu of options ;
menu();
//Determine users requirements
menu_choice(&option);
//Perform users specified option
switch(option)
{
case 1: //Enter information for all players
player_input(player);
break;
case 2: //display table for all players
goal_table(player);
break;
case 3: //update specific information for a player
goal_update(player);
break;
case 4: //exit from program
break;
default://illegal input
printf ("\n\n This is not an available option\nAvailable options are
1,2,3,4");
}
} while (option !=4);
}
init_player(player)
//------------------
struct football player[];
{
int i,j;
for (i=0;i<100;++i)
{
for (j=0;j<30;++j)
{
player.name[j]=' ';
player.team[j]=' ';
}
player.goals=0;
}
}
menu()
//------------------
{
system ("clear");
printf("\n\n football system ");
printf("\n ------------------- ");
printf("\n\n 1- Enter information ");
printf("\n\n 2- Display table for all players");
printf("\n\n 3- Update specific information");
printf("\n\n 4- Exit from program ");
}
menu_choice(opt)
//------------------
int *opt;
{
printf("\n\n Select one of the above (1-4) : ");
scanf("%d",opt);
//clear screen
system("clear");
}
player_input(player)
//------------------
struct football player[];
{
int i;
char more;
i=-1;
do
{
++i;
//input players name
printf("Enter players name : ");
input_string(player.name);
//input players team
printf("Enter players team : ");
input_string(player.team);
//input number of goals scored
fflush(stdin);
printf("Enter number of goals scored: ");
scanf("%d",&player.goals);
printf("\n %-30s %-30s %4d\n",player.name,player.team,player.goals);
printf("\n %d I Value inside insert\n ",i);
//more players
if (i<99)
{
printf("More playersd to be entered (y/n)---->");
scanf("%s",&more);
}
} while (more == 'y' && i<99);
//Terminate players list
if (i<99) player[i+1].goals=-1;
}
input_string(alpha)
//------------------
char *alpha;
{
int i;
i=-1;
//Flush the keyboard buffer
fflush(stdin);
do
{
++i;
//input a character
alpha=getchar();
} while (alpha !='\n' && i<29);
//Terminate string
alpha='\0';
}
goal_table(player) //Display table of goals scored
//------------------
struct football player[];
{
int i=0;
char cont;
//Output table headings
printf("\n\n Name Team Goals");
printf("\n\n ---- ---- -----");
do
{
//Output player information
printf("\n %d I Value inside display ",i);
printf("\n %-30s %-30s %4d\n",player.name,player.team,player.goals);
++i;
} while (i<=2);
printf("\n\n Press C to continue ");
scanf("%s",&cont);
}
goal_update(player) //update table of goals scored
//------------------
struct football player[];
{
char name[30];
int i,match,goal;
//input players name to be updated
printf("Enter name of player");
input_string(name);
//Find players record
i=0;
while ((player.goals!=-1)&&(i<100)&&(match=strcmp(name,player.name)
!=0))
++i;
//Input number of goals to be added
printf("Enter number of goals to be added to players account");
scanf("%d",&goal);
//Update players record
if (match==0)
player.goals=player.goals+goal;
else
printf("\n\n Player %s is not in the goal table \n",name);
}