| | |
ootball league table - add data
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
>so what do i need to use instead of the fflush(null); line??
It's fine to use
http://www.daniweb.com/tutorials/tutorial45806.html
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.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Mar 2007
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
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]
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:
c Syntax (Toggle Plain Text)
#include<stdio.h> #include<string.h> #include<stdlib.h> #define MAX 8 #define TRUE 1 #define FALSE 0 char print_header(void); void draw_table (void); struct TeamInfo { char team[20]; int played; int points; int goalsf; int goalsa; int goalsd; } ; #define MAX_ALLOWED_TEAMS 20 struct TeamInfo arrdetails[MAX_ALLOWED_TEAMS] ; void take_team_input(); void take_team_name(); /************************************************************/ /* Main Function */ /************************************************************/ main() /**** main is an INT ****/ { char option, rtn; option = print_header(); while(option != 'q' && option != 'Q') { /**** display your menu ****/ /**** accept option ****/ switch(option) { case 'a': case 'A': /*Enter team */ printf("\nPlease Enter a new team:\n"); take_team_name(); break; case 'b': case'B': /*Display table*/ printf("\nLeague Table:\n"); draw_table(); break; case 'c': case'C': /*Match details*/ //just call the function that does what you want in this case.. take_team_input(); break; case 'd': case 'D': /*Exit*/ printf("\n*** FINISHED ***\n"); exit(0); default: /*invalid option letter entererd*/ printf("\nInvalid choice, Please try again\n"); } option = print_header(); } return 0; } /****************************************************************/ /* Functions */ /****************************************************************/ /****************************************************************/ /* Print Menu Options */ /****************************************************************/ char print_header(void) { char opt,rtn; printf("\n****************************"); printf("\n* Football League *"); printf("\n****************************\n"); printf("\na.\tEnter a New Team\n"); printf("\nb.\tDisplay Current Table\n"); printf("\nc.\tEnter Match Details\n"); printf("\nd.\tExit System\n"); printf("\nPlease Enter Your Choice: \n"); scanf("%c%c",&opt,&rtn); return opt; } /****************************************************************/ /* Enters Team Name */ /****************************************************************/ void take_team_name() { int current_empty_slot = 0 ; 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(stdout); scanf( "%s", arrdetails[current_empty_slot].team ); //now ask if he wants to enter any more... printf("\n\nWant To Enter Anymore Team Names? [y/n]: "); fflush(stdout); scanf( "%c", choice ); current_empty_slot++; if(MAX_ALLOWED_TEAMS <= current_empty_slot) { printf("\nMax limit for team entries reached.", ); return ; } } } /****************************************************************/ /* Enters Match Details */ /****************************************************************/ void take_team_input() { int current_empty_slot = 0 ; 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(stdout); scanf( "%s", arrdetails[current_empty_slot].team ); printf("\nEnter number of games played: "); fflush(stdout); scanf( "%d", arrdetails[current_empty_slot].played ); printf("\nEnter goals for: "); fflush(stdout); 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(stdout); scanf( "%c", choice ); current_empty_slot++; } } /****************************************************************/ /* Displays the current teams */ /* And Info */ /****************************************************************/ void draw_table (void) { int i; // Displays Sorted League Table for ( i = 0; i < MAX; i++ ) { 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); } }
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.
•
•
Join Date: Mar 2007
Posts: 12
Reputation:
Solved Threads: 0
got another quick thing
if ive got the code
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?
if ive got the code
c Syntax (Toggle Plain Text)
printf("\nWin, Loss or Draw "); fflush(stdout); 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.
•
•
•
•
within the while loop i added another if loop so that is checks ...

•
•
•
•
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.•
•
•
•
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
Looking at your code:
c Syntax (Toggle Plain Text)
main() /**** main is an INT ****/
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Other Threads in the C Forum
- Previous Thread: linked list
- Next Thread: big o notation?
| Thread Tools | Search this Thread |
#include * append array arrays asterisks bash binarysearch calculate changingto char character cm copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators input intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf opensource openwebfoundation overwrite owf pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprogramming standard strchr string systemcall testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






