ootball league table - add data

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: ootball league table - add data

 
0
  #11
Mar 5th, 2007
>so what do i need to use instead of the fflush(null); line??

It's fine to use fflush(stdout) to clear the output buffer, however for input, you need to use better input techniques.

http://www.daniweb.com/tutorials/tutorial45806.html
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 12
Reputation: fightfox06 is an unknown quantity at this point 
Solved Threads: 0
fightfox06 fightfox06 is offline Offline
Newbie Poster

Re: ootball league table - add data

 
0
  #12
Mar 6th, 2007
Originally Posted by thekashyap View Post

void take_team_input()
{
char choice = 'Y' ;

if( MAX_ALLOWED_TEAMS <= current_empty_slot )
{
printf( "\nMax limit for team entries reached.", ) ;
return ;
}

while( ('y' == choice) || ('Y' == choice) )
{
//take the input:
printf("\nTaking input for team number: %d", current_empty_slot ) ;
printf("\nEnter team name: ") ; fflush(null) ;
scanf( "%s", arrdetails[current_empty_slot].team ) ;
printf("\nEnter number of games played: ") ; fflush(null) ;
scanf( "%d", arrdetails[current_empty_slot].played ) ;
printf("\nEnter goals for: ") ; fflush(null) ;
scanf( "%d", arrdetails[current_empty_slot].goalsf ) ;
//...... and so on...
//now ask if he wants to enter any more...
printf("\n\nWant to enter details for any more teams? [y/n]: ") ; fflush(null) ;
scanf( "%c", choice ) ;

current_empty_slot++ :
}
}
[/code]
within the while loop i added another if loop so that is checks the max number of teams to see if the limit has been reached. the first loop you had only checks the initial value which is always going to be zero, once ive entered all the teams details and go through add teams again it starts from zero because thats wat the initial value is always set 2.

in my code now ive removed the insersion peice because if im going to be able to add team into manually i dont need preset data.

also for now ive removed the sort untill ive got everything else sorted.

my current code is:

  1.  
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5. #define MAX 8
  6. #define TRUE 1
  7. #define FALSE 0
  8.  
  9. char print_header(void);
  10. void draw_table (void);
  11.  
  12. struct TeamInfo
  13. {
  14. char team[20];
  15. int played;
  16. int points;
  17. int goalsf;
  18. int goalsa;
  19. int goalsd;
  20. } ;
  21.  
  22. #define MAX_ALLOWED_TEAMS 20
  23. struct TeamInfo arrdetails[MAX_ALLOWED_TEAMS] ;
  24.  
  25. void take_team_input();
  26. void take_team_name();
  27.  
  28.  
  29. /************************************************************/
  30. /* Main Function */
  31. /************************************************************/
  32. main() /**** main is an INT ****/
  33. {
  34. char option, rtn;
  35. option = print_header();
  36. while(option != 'q' && option != 'Q')
  37. {
  38. /**** display your menu ****/
  39. /**** accept option ****/
  40. switch(option)
  41. {
  42. case 'a': case 'A': /*Enter team */
  43. printf("\nPlease Enter a new team:\n");
  44. take_team_name();
  45. break;
  46. case 'b': case'B': /*Display table*/
  47. printf("\nLeague Table:\n");
  48. draw_table();
  49. break;
  50. case 'c': case'C': /*Match details*/
  51. //just call the function that does what you want in this case..
  52. take_team_input();
  53. break;
  54. case 'd': case 'D': /*Exit*/
  55. printf("\n*** FINISHED ***\n");
  56. exit(0);
  57. default: /*invalid option letter entererd*/
  58. printf("\nInvalid choice, Please try again\n");
  59. }
  60. option = print_header();
  61. }
  62. return 0;
  63. }
  64.  
  65. /****************************************************************/
  66. /* Functions */
  67. /****************************************************************/
  68. /****************************************************************/
  69. /* Print Menu Options */
  70. /****************************************************************/
  71. char print_header(void)
  72. {
  73. char opt,rtn;
  74. printf("\n****************************");
  75. printf("\n* Football League *");
  76. printf("\n****************************\n");
  77. printf("\na.\tEnter a New Team\n");
  78. printf("\nb.\tDisplay Current Table\n");
  79. printf("\nc.\tEnter Match Details\n");
  80. printf("\nd.\tExit System\n");
  81. printf("\nPlease Enter Your Choice: \n");
  82. scanf("%c%c",&opt,&rtn);
  83. return opt;
  84. }
  85.  
  86.  
  87. /****************************************************************/
  88. /* Enters Team Name */
  89. /****************************************************************/
  90. void take_team_name()
  91. {
  92. int current_empty_slot = 0 ;
  93. char choice = 'Y' ;
  94.  
  95. if(MAX_ALLOWED_TEAMS <= current_empty_slot)
  96. {
  97. printf("\nMax limit for team entries reached.", );
  98. return ;
  99. }
  100.  
  101. while(('y' == choice) || ('Y' == choice))
  102. {
  103. //take the input:
  104. printf("\nTaking input for team number: %d", current_empty_slot );
  105.  
  106. printf("\nEnter Team Name: ");
  107. fflush(stdout);
  108. scanf( "%s", arrdetails[current_empty_slot].team );
  109.  
  110. //now ask if he wants to enter any more...
  111. printf("\n\nWant To Enter Anymore Team Names? [y/n]: ");
  112. fflush(stdout);
  113. scanf( "%c", choice );
  114.  
  115. current_empty_slot++;
  116.  
  117. if(MAX_ALLOWED_TEAMS <= current_empty_slot)
  118. {
  119. printf("\nMax limit for team entries reached.", );
  120. return ;
  121. }
  122. }
  123.  
  124. }
  125.  
  126. /****************************************************************/
  127. /* Enters Match Details */
  128. /****************************************************************/
  129. void take_team_input()
  130. {
  131. int current_empty_slot = 0 ;
  132. char choice = 'Y' ;
  133.  
  134. if(MAX_ALLOWED_TEAMS <= current_empty_slot)
  135. {
  136. printf("\nMax limit for team entries reached.", );
  137. return ;
  138. }
  139.  
  140. while(('y' == choice) || ('Y' == choice))
  141. {
  142. //take the input:
  143. printf("\nTaking input for team number: %d", current_empty_slot );
  144.  
  145. printf("\nEnter team name: ");
  146. fflush(stdout);
  147. scanf( "%s", arrdetails[current_empty_slot].team );
  148.  
  149. printf("\nEnter number of games played: ");
  150. fflush(stdout);
  151. scanf( "%d", arrdetails[current_empty_slot].played );
  152.  
  153. printf("\nEnter goals for: ");
  154. fflush(stdout);
  155. scanf( "%d", arrdetails[current_empty_slot].goalsf );
  156. //...... and so on...
  157. //now ask if he wants to enter any more...
  158. printf("\n\nWant to enter details for any more teams? [y/n]: ");
  159. fflush(stdout);
  160. scanf( "%c", choice );
  161.  
  162. current_empty_slot++;
  163. }
  164.  
  165. }
  166.  
  167. /****************************************************************/
  168. /* Displays the current teams */
  169. /* And Info */
  170. /****************************************************************/
  171. void draw_table (void)
  172. {
  173. int i;
  174. // Displays Sorted League Table
  175. for ( i = 0; i < MAX; i++ )
  176. {
  177. printf("%20s \t %d \t %d \t %d \t \t %d\n",arrdetails[i].team,arrdetails[i].played,arrdetails[i].points,arrdetails[i].goalsf,arrdetails[i].goalsa);
  178. }
  179. }

got a the problem mentioned above with the MAX_ALLOWED_TEAMS always being reset to zero when the add team name option is selected, also it only allows me to enter a single word if i enter more than one it messes up the whole thing.

also when adding the match details i need to enter the team to add the details for and instead of creating a new peice of data in the array i need it to add the data to the current data that is already there

thanks everyone for there help its much appreachiated and im starting to understand c alot more now and slowly making sum progress on my own even tho im still getting alot of help from you.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 12
Reputation: fightfox06 is an unknown quantity at this point 
Solved Threads: 0
fightfox06 fightfox06 is offline Offline
Newbie Poster

Re: ootball league table - add data

 
0
  #13
Mar 6th, 2007
got another quick thing

if ive got the code

  1. printf("\nWin, Loss or Draw ");
  2. fflush(stdout);
  3. scanf( "%d", arrdetails[current_empty_slot].points );

if i wanted to input a string from the user and change Win into 3 points added to points, 0 point added for loss and 1 point added to draw would i just use a swich statement with each case being win loss and draw then just within the case add the relivent points depending on wat the user selects?
Last edited by fightfox06; Mar 6th, 2007 at 5:50 am.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: ootball league table - add data

 
0
  #14
Mar 6th, 2007
Originally Posted by fightfox06 View Post
within the while loop i added another if loop so that is checks ...
it's an if statement. IF is not a loop...


Originally Posted by fightfox06 View Post
got a the problem mentioned above with the MAX_ALLOWED_TEAMS always being reset to zero when the add team name option is selected, also it only allows me to enter a single word if i enter more than one it messes up the whole thing.
MAX_ALLOWED_TEAMS cannot be reset to 0. That's impossible. The problem is your use of scanf(). This series will explain both what the problem is and how to fix it.



Originally Posted by fightfox06 View Post
also when adding the match details i need to enter the team to add the details for and instead of creating a new peice of data in the array i need it to add the data to the current data that is already there
So you enter the team number, search for it, display the team name, and accept the new data, and load it into the structure.


Looking at your code:
  1. main() /**** main is an INT ****/
are you ignoring the comment? It's important!
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC