Thread: Personal Program that is making me go wtf?

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    14

    Question Personal Program that is making me go wtf?

    Hello all,

    Ok, I'm trying to make a program that plays a soccer league using random number generators to allocate goals to playing sides. That is all working without any problems. My problem is, when I try to update the file, it just overrides the file with nothing! In my program I am trying to update the team statisitics after a round of games has been played. This is a simplified version of what I am trying to do...

    in my functions.h file...

    Code:
    #include <stdio.h>																					
    #include <stdlib.h>
    #include <string.h>
    
    
    
    
    struct players{
    	
    	char *familyName;  
    	char *firstName;
    	int playerNo;
    	int noSeasonsPlayed;
    	int noGamesPlayed;
    	int noGoals;
    	int worth;
    	int ID;
    
    };
    
    typedef struct players PlayerInfo;
    
    void displayPlayerInfoFamily (PlayerInfo [], int);
    void outputFile(FILE *, PlayerInfo [], int); 
    int readFilePlayers ( FILE *, PlayerInfo []);
    This is my functions.c file....

    Code:
    #include "functions.h"
    
    int readFilePlayers(FILE *PdataR, PlayerInfo players[]) {		
    
    
    	char familyName[20];							
    	char firstName[20];							
    
    	int read = 0;						
    
    	fscanf(PdataR, "%s", familyName);
    
    
    	while (read < 3 && !feof(PdataR)) {
    
    		players[read].familyName = malloc (sizeof(char) * (strlen(familyName) + 1));
    		strcpy(players[read].familyName, familyName);									
    		fscanf(PdataR,"%s", firstName);
    		
    		players[read].firstName = malloc (sizeof(char) * (strlen(firstName) + 1));
    		strcpy(players[read].firstName, firstName);
    		fscanf(PdataR, "%d %d %d %d %d %d", &players[read].playerNo, &players[read].noSeasonsPlayed, &players[read].noGamesPlayed, 
    			   &players[read].noGoals, &players[read].worth, &players[read].ID);   
    
    		
    		read++;
    
    		fscanf(PdataR, "%s", familyName);											
    
    	}
    	fclose(PdataR);
    
    	return read;
    	
    
    	
    }	//End of readFilePlayers function 
    
    
    
    void displayPlayerInfoFamily (PlayerInfo players[], int noOfPlayers) {
    
    	int print = 0;
    	char familyNameIn[22];
    
    	printf("Please enter the surname of the player: ");
    	scanf("%s", &familyNameIn);
    	printf("\n");
    	
    	
    	for (print = 0; print < noOfPlayers; print++) {	
    			
    		if (strcmp(familyNameIn, players[print].familyName) == 0) {
    
    			
    			printf("Player statistics \n");
    			printf("==================================================\n");
    			
    			printf("||Surname: %22s \n", players[print].familyName);
    			printf("||Initial: %17s \n", players[print].firstName); 
    			printf("||Player Position:%10d \n",players[print].playerNo);
    			printf("||Seasons Played: %10d \n", players[print].noSeasonsPlayed);
    			printf("||Games Played: %12d \n", players[print].noGamesPlayed);
    			printf("||Number of goals scored: %2d \n", players[print].noGoals);
    			printf("||Worth ($M): %14d \n", players[print].worth);
    			printf("||Player ID:%17d \n", players[print].ID); 
    			printf("||________________________________________________\n");
    		}
    
    		
    	}
    	
    	printf("\n");
    
    }
    
    void outputFile(FILE *PdataW, PlayerInfo players[], int noOfPlayers) {
    
    /* Runs when the user wishes to exit your program */		
    	
    	int write = 0;
    		printf("HERE1!\n");
    		
    	/* loop control, used for sending information to the file */
    
    	if (PdataW  == NULL) {
    			printf("HERE1!\n");
    	/* checks if the file exists */
    		printf("Error, this file does not exist!");
    		exit(0);
    	}
    
    	else {
    	/* if it does... */
    			printf("HERE2!\n");
    			printf("noOfPlayers is: %d", noOfPlayers);
    			printf("write is: %d", write);
            
    		while(write < noOfPlayers && !feof(PdataW)) {
    			/* Prints the array information to the file */
    			printf("HERE1!\n");
    			fprintf(PdataW,"%s", players[write].familyName);
    			printf("HERE3\n");
    			fprintf(PdataW, " "); 
    			printf("HERE4\n");
    			/* This is so there is always a space between the player's first and last name, no matter what the size */
    			fprintf(PdataW,"%s\n", players[write].firstName);
    			printf("HERE5\n");
    			fprintf(PdataW,"%-d %-d %-d %-d %-d %-d\n", players[write].playerNo, players[write].noSeasonsPlayed, 
    				    players[write].noGamesPlayed, players[write].noGoals, players[write].worth, players[write].ID);
    			printf("HERE6\n");
    			
    		
    
    
    
    
    			write++;
    			printf("write is: %d", write);
    			/* updating the array position */
    			
    			
    		}  // end while
    	}     // end else 
    	printf("HERE7\n");
    }		// end of outputFile function
    this is my main.c....

    Code:
    #include "functions.h"
    
    int main () {
    
    
    	FILE *PdataR = fopen("players.txt", "r");
    	FILE *PdataW = fopen("players.txt", "w");
    	
    	PlayerInfo players[2];
    	
    	int noOfPlayers;
    
    	noOfPlayers = readFilePlayers(PdataR, players);
    	
    	
    
    	printf("%d", noOfPlayers);
    
    	displayPlayerInfoFamily(players, noOfPlayers);
    
    	
    }
    I know that this does not ask me to enter a new name or value or anything, but it is just wiping the text file before it even reads it in. where do I have to close PdataR and open RdataW?

    Cheers,

    Submeg

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Quote Originally Posted by Submeg
    Code:
    	FILE *PdataR = fopen("players.txt", "r");
    	FILE *PdataW = fopen("players.txt", "w");
    The second line completely wipes players.txt. You shouldn't read and write from one file at the same time. Try writing to a different file and then when you're finished, copying that file over the original.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    14
    I figured that, so just make the FP point to a new file, then do a readFile for that file and then outputFile to the original? So there is no way to use fclose to free the file for writing?

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Is there any reason why you cannot open the file in read/write mode?

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    14
    I tried that, but it didnt seem to work! It wouldn't update it! would this be instead:

    FILE *PdataR = fopen("players.txt", "rw");

    and then in the outputFile I use the PdataR FP?

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    I believe for read/write mode, you have to set the second parameter of fopen() to "r+".

    Besides, you can change this:
    Code:
    struct players{
    	/* ... */
    };
    
    typedef struct players PlayerInfo;
    To this:
    Code:
    typedef struct players{
    	/* ... */
    } PlayerInfo;

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You should have a look at your documentation some time. Here is an excerpt from my man page on fopen:

    r Open text file for reading. The stream is positioned at the beginning of the file.

    r+ Open for reading and writing. The stream is positioned at the beginning of the file.

    w Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.

    w+ Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of
    the file.

    a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.

    a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at
    the beginning of the file, but output is always appended to the end of the file.
    Check out the whole man (3) fopen entry...
    Last edited by kermit; 06-25-2006 at 07:34 PM.

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    14
    Ok, I have added a player to the array, and have save it into another file, but is there an easy to do a file copy? using "rw" is still sending it to the new file....hmm...

    The reason I havent read the documentation is that I'm studying this course at Uni, so you know how it goes....you work at it until it works and does what you want, you dont learn any extra! lol.

  9. #9
    Registered User
    Join Date
    Jun 2006
    Posts
    14
    Ok so here are my functions now(had to add alot to search and add etc...)

    functions.h....

    Code:
    #include <stdio.h>																					
    #include <stdlib.h>
    #include <string.h>
    
    #define STRING_LENGTH 22
    #define MAX_PLAYERS 10
    
    
    
    struct players{
    	
    	char *familyName;  
    	char *firstName;
    	int playerNo;
    	int noSeasonsPlayed;
    	int noGamesPlayed;
    	int noGoals;
    	int worth;
    	int ID;
    
    };
    
    typedef struct players PlayerInfo;
    
    void displayPlayerInfoFamily (PlayerInfo [], int);
    void outputFile(FILE *, PlayerInfo [], int); 
    void display (PlayerInfo [], int);
    
    int readFilePlayers ( FILE *, PlayerInfo []);
    int addPlayer (PlayerInfo [], char *, char *, int *); 
    
    int addPlayer (PlayerInfo [], char *, char *, int *); 
    int searchPlayer (char *, char *, PlayerInfo [], int);
    int searchAddPlayer (char *, char *, PlayerInfo [], int);
    functions.c....

    [CODE]

    #include "functions.h"

    int readFilePlayers(FILE *PdataR, PlayerInfo players[]) {


    char familyName[20];
    char firstName[20];

    int read = 0;

    fscanf(PdataR, "%s", familyName);


    while (read < 3 && !feof(PdataR)) {

    players[read].familyName = malloc (sizeof(char) * (strlen(familyName) + 1));
    strcpy(players[read].familyName, familyName);
    fscanf(PdataR,"%s", firstName);

    players[read].firstName = malloc (sizeof(char) * (strlen(firstName) + 1));
    strcpy(players[read].firstName, firstName);
    fscanf(PdataR, "%d %d %d %d %d %d", &players[read].playerNo, &players[read].noSeasonsPlayed, &players[read].noGamesPlayed,
    &players[read].noGoals, &players[read].worth, &players[read].ID);


    read++;

    fscanf(PdataR, "%s", familyName);

    }
    fclose(PdataR);

    return read;



    } //End of readFilePlayers function



    void displayPlayerInfoFamily (PlayerInfo players[], int noOfPlayers) {

    int print = 0;
    char familyNameIn[22];

    printf("Please enter the surname of the player: ");
    scanf("%s", &familyNameIn);
    printf("\n");


    for (print = 0; print < noOfPlayers; print++) {

    if (strcmp(familyNameIn, players[print].familyName) == 0) {


    printf("Player statistics \n");
    printf("========================================== ========\n");

    printf("||Surname: %22s \n", players[print].familyName);
    printf("||Initial: %17s \n", players[print].firstName);
    printf("||Player Position:%10d \n",players[print].playerNo);
    printf("||Seasons Played: %10d \n", players[print].noSeasonsPlayed);
    printf("||Games Played: %12d \n", players[print].noGamesPlayed);
    printf("||Number of goals scored: %2d \n", players[print].noGoals);
    printf("||Worth ($M): %14d \n", players[print].worth);
    printf("||Player ID:%17d \n", players[print].ID);
    printf("||________________________________________ ________\n");
    }


    }

    printf("\n");

    }


    int searchAddPlayer(char *familyNameAdd, char *firstNameAdd, PlayerInfo players[], int noOfPlayers) {
    /* runs when the user wishes to search for a player */

    int print;
    int searchID;

    for (print = 0; print < noOfPlayers; print++) {

    if (strcmp(familyNameAdd, players[print].familyName) == 0) {
    return print;
    }
    }


    searchID = -2;
    return searchID;
    /* If the name is not found, then a negative value is returned */

    } // End of searchPlayer function


    int searchAdd (char *familyNameAdd, char *firstNameAdd, PlayerInfo players[], int noOfPlayers ) {
    /* This is called to search the array for the player, and if not found, determines where it should enter the array */
    int enterPos = 0;
    /* Loop control for where the player should be entered in the predetermined array */
    int index = 0;
    /* Loop control to cycle through the players array */

    enterPos = searchAddPlayer(familyNameAdd, firstNameAdd, players, noOfPlayers);
    /* First, it has to be determined if the player already exists */


    if (enterPos >= 0) {

    printf("Player already exists! \n");
    enterPos = -2;
    /*This control is: if the searchPlayer function returns a number greater than -1, then the player exists. If it returns
    a number less than zero, then the player doesn't exist. The enterPos is then set to -2, which I have decided to mean that
    the player already exists and cannot be entered into the loop */

    }

    else { // ELSE LOOP 1
    /* Otherwise it goes on to determine where the player has to be inserted, these loops get hard to follow
    so description included (I talked with my fellow students about the basic loop structure for else loop 1)*/

    for (index = 0; index < noOfPlayers; index++) { // FOR LOOP 1
    /* This loop control is used to cycle through the array to help determine where the player must be added */
    int compareFamily;
    int compareFirst;

    /* These variables are used to store the result of the strcmp so the player to be inserted can be placed in the
    right position */

    compareFamily = strcmp(familyNameAdd, players[index].familyName);
    /* does a strcmp for the entered family name with all the family names in the array */


    if(compareFamily < 0) { // IF LOOP 1
    /* This is: is the family name BEFORE the one being compared to in the array? */

    if (index == 0 && compareFamily !=0) { // IF LOOP 2
    enterPos = 0;
    /*This is for the case when you want to add a player into the very first position in the players array */
    } // end of if loop 2

    return enterPos;
    /* returning the current position so that the player can be added there */

    } // end of if loop 1


    else if(compareFamily > 0) { // ELSE IF 1
    /* This is: is the family name AFTER the one being compared to in the array? */
    enterPos = index + 1;
    /* If so, move to the next player */

    } // end of else if loop 1

    else if (compareFamily == 0) { // ELSE IF 2
    /* This is: is the family name the SAME as the one being compared to in the array? */

    compareFirst = strcmp(firstNameAdd, players[index].firstName);
    /* If so, then compare the first name and do the previous steps again */


    if(compareFirst < 0) { // IF LOOP 3
    /* This is: is the first name BEFORE the first name being compared? */
    return enterPos;

    } // end of if loop 3


    else if (compareFirst > 0) { //ELSE IF 3

    enterPos = index + 1 ;

    } // end of else if loop 3
    } // end of else if loop 2
    } // end of for loop 1
    } // end of else loop 1

    return enterPos;

    } // end of searchAdd function




    int addPlayer (PlayerInfo players[], char *familyNameIn, char *firstNameIn, int *noOfPlayers) {

    char familyNameAdd[STRING_LENGTH];
    char firstNameAdd[STRING_LENGTH];

    int numOfPlayer = 0;
    int enterPos;
    int move = 0;
    int familyNameLen; /* used to store the length of the entered family name */
    int firstNameLen;


    printf("Please enter the surname of the player you wish to add, 0 to exit: ");
    scanf("%s", familyNameAdd);

    familyNameLen = strlen(familyNameAdd);
    /* This discovers how long the family name is */


    if (strcmp(familyNameAdd,"0")) { //if loop 1

    printf("Please enter the first name of the player you wish to add, 0 to exit: ");
    scanf("%s", firstNameAdd);
    firstNameLen = strlen(firstNameAdd);

    if (strcmp(firstNameAdd,"0") ) { //if loop 2

    printf("\n");

    enterPos = searchAdd (familyNameAdd, firstNameAdd, players, *noOfPlayers );
    /*calls the searchAdd function so that the position of the player is able to be determined */

    if (*noOfPlayers == 0) {

    enterPos = 0;

    }
    /*When the array is empty, the enterPos has to be set to zero so that it can be entered into the first position in
    the array */

    if (*noOfPlayers < MAX_PLAYERS && enterPos != -2 && familyNameLen < 21 && firstNameLen < 21) {
    /* This checks if the array is full or not, if it isnt full, you can add to the array. Also checks if the has been
    decided if the player already exists or the name is too long to fit into the given space */


    for(move = *noOfPlayers - 1; move >= enterPos; move--) {
    /* The moving of players only occurs when the player being accessed is higher in the alphabet than the player
    that is going to be entered */

    players[move+1].familyName = players[move].familyName;
    players[move+1].firstName = players[move].firstName;
    players[move+1].playerNo = players[move].playerNo;
    players[move+1].noSeasonsPlayed = players[move].noSeasonsPlayed;
    players[move+1].noGamesPlayed = players[move].noGamesPlayed;
    players[move+1].noGoals = players[move].noGoals;
    players[move+1].worth = players[move].worth;
    players[move+1].ID = players[move].ID+1;
    /*This block of code moves the player at the position being accessed by move to move + 1. That is, it moves
    it one space down the array */

    } // end of for loop

    players[enterPos].familyName = malloc (sizeof(char) * (strlen(familyNameAdd) + 1));
    strcpy(players[enterPos].familyName, familyNameAdd);

    players[enterPos].firstName = malloc (sizeof(char) * (strlen(firstNameAdd) + 1));
    strcpy(players[enterPos].firstName, firstNameAdd);

    players[enterPos].playerNo = 0;
    players[enterPos].noSeasonsPlayed = 0;
    players[enterPos].noGamesPlayed = 0;
    players[enterPos].noGoals = 0;
    players[enterPos].worth = 0;
    players[enterPos].ID = enterPos;
    /*This block of code makes a player with the entered family and first names and gives all int values a value of
    0 */

    *noOfPlayers = *noOfPlayers + 1;
    /*The number of players is then updated */
    printf("\nPlayer has been added to the array.\n\n");

    } // end if

    else
    printf("You cannot add to this array! \n\n");
    } // end of if 2
    } // end of if 1

    return enterPos;
    return *noOfPlayers;


    } // end of addPlayer function


    void display(PlayerInfo players[MAX_PLAYERS], int noOfPlayers) {

    char last[2];
    char inital[2];
    char search[8];
    int print = 0;
    int seasons = 0;
    int games = 0;
    int goals = 0;
    int worth = 0;

    printf("noOfPlayers: %d\n", noOfPlayers);
    printf("How do you wish to list the player(s)?\n");
    printf("Surname, Initial, Seasons, Games, Goals or Worth?");
    scanf("%s", &search);


    if (strcmp(search, "Seasons") == 0) {

    printf("How many seasons has the player(s) played? ");
    scanf("%d", &seasons);
    printf("\n");

    printf("\n======================================== ========================================");
    printf("|| Players ||");
    printf("========================================== ======================================");
    printf("|| ||");
    printf("||Last Inital| PlayerNo Seasons Games noGoals worth ID ||");
    printf("||________________________________________ ____________________________________||");

    for (print = 0; print < noOfPlayers; print++) {

    if (players[print].noSeasonsPlayed == seasons) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");

    }
    }

    }

    if (strcmp(search, "Games") == 0) {

    printf("How many games has the player(s) played? ");
    scanf("%d", &games);
    printf("\n");

    printf("\n======================================== ========================================");
    printf("|| Players ||");
    printf("========================================== ======================================");
    printf("|| ||");
    printf("||Last Inital| PlayerNo Seasons Games noGoals worth ID ||");
    printf("||________________________________________ ____________________________________||");

    for (print = 0; print < noOfPlayers; print++) {

    if (players[print].noGamesPlayed == games) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");

    }
    }

    }

    if (strcmp(search, "Goals") == 0) {

    printf("How many goals has the player(s) scored? ");
    scanf("%d", &goals);
    printf("\n");

    printf("\n======================================== ========================================");
    printf("|| Players ||");
    printf("========================================== ======================================");
    printf("|| ||");
    printf("||Last Inital| PlayerNo Seasons Games noGoals worth ID ||");
    printf("||________________________________________ ____________________________________||");

    for (print = 0; print < noOfPlayers; print++) {

    if (players[print].noGoals == goals) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");

    }
    }

    }

    if (strcmp(search, "Worth") == 0) {

    printf("How much is the player(s) worth? ");
    scanf("%d", &worth);
    printf("\n");

    printf("\n======================================== ========================================");
    printf("|| Players ||");
    printf("========================================== ======================================");
    printf("|| ||");
    printf("||Last Inital| PlayerNo Seasons Games noGoals worth ID ||");
    printf("||________________________________________ ____________________________________||");

    for (print = 0; print < noOfPlayers; print++) {

    if (players[print].worth == worth) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");

    }
    }

    }



    if (strcmp(search, "Surname")==0) {

    printf("Please enter the first letter of their last name: ");
    scanf("%s", &last);

    printf("\n======================================== ========================================");
    printf("|| Players ||");
    printf("========================================== ======================================");
    printf("|| ||");
    printf("||Last Inital| PlayerNo Seasons Games noGoals worth ID ||");
    printf("||________________________________________ ____________________________________||");

    if(strcmp(last, "A")==0) {

    for (print = 0; print < noOfPlayers; print++) {

    if (strncmp(players[print].familyName, "A",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "B")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strncmp(players[print].familyName, "B",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "C")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strncmp(players[print].familyName, "C",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "D")==0) {

    for (print = 0; print < noOfPlayers; print++) {

    if (strncmp(players[print].familyName, "D",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "E")==0) {

    for (print = 0; print < noOfPlayers; print++) {

    if (strncmp(players[print].familyName, "E",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "F")==0) {

    for (print = 0; print < noOfPlayers; print++) {

    if (strncmp(players[print].familyName, "F",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "G")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strncmp(players[print].familyName, "G",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "H")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strncmp(players[print].familyName, "H",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "I")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strncmp(players[print].familyName, "I",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "J")==0) {

    for (print = 0; print < noOfPlayers; print++) {

    if (strncmp(players[print].familyName, "J",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "K")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strncmp(players[print].familyName, "K",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "L")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strncmp(players[print].familyName, "L",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "M")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strncmp(players[print].familyName, "M",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "N")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strncmp(players[print].familyName, "N",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "O")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strncmp(players[print].familyName, "O",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "P")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strncmp(players[print].familyName, "P",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "Q")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strncmp(players[print].familyName, "Q",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "R")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strncmp(players[print].familyName, "R",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "S")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strncmp(players[print].familyName, "S",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "T")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strncmp(players[print].familyName, "T",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "U")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strncmp(players[print].familyName, "U",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "V")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strncmp(players[print].familyName, "V",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "W")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strncmp(players[print].familyName, "W",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "X")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strncmp(players[print].familyName, "X",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(last, "Y")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strncmp(players[print].familyName, "Y",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else {

    for (print = 0; print < noOfPlayers; print++) {


    if (strncmp(players[print].familyName, "Z",1) == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }
    } // end of list by surname

    if (strcmp(search, "Initial")==0) {

    printf("Please enter their initial: ");
    scanf("%s", &inital);

    printf("\n======================================== ========================================");
    printf("|| Players ||");
    printf("========================================== ======================================");
    printf("|| ||");
    printf("||Last Inital| PlayerNo Seasons Games noGoals worth ID ||");
    printf("||________________________________________ ____________________________________||");

    if(strcmp(inital, "A")==0) {

    for (print = 0; print < noOfPlayers; print++) {



    if (strcmp(players[print].firstName, "A") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "B")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strcmp(players[print].firstName, "B") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "C")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strcmp(players[print].firstName, "C") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "D")==0) {

    for (print = 0; print < noOfPlayers; print++) {

    if (strcmp(players[print].firstName, "D") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "E")==0) {

    for (print = 0; print < noOfPlayers; print++) {

    if (strcmp(players[print].firstName, "E") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "F")==0) {

    for (print = 0; print < noOfPlayers; print++) {

    if (strcmp(players[print].firstName, "F") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "G")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strcmp(players[print].firstName, "G") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "H")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strcmp(players[print].firstName, "H") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "I")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strcmp(players[print].firstName, "I") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "J")==0) {

    for (print = 0; print < noOfPlayers; print++) {

    if (strcmp(players[print].firstName, "J") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "K")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strcmp(players[print].firstName, "K") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "L")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strcmp(players[print].firstName, "L") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "M")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strcmp(players[print].firstName, "M") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "N")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strcmp(players[print].firstName, "N") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "O")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strcmp(players[print].firstName, "O") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "P")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strcmp(players[print].firstName, "P") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "Q")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strcmp(players[print].firstName, "Q") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "R")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strcmp(players[print].firstName, "R") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "S")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strcmp(players[print].firstName, "S") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "T")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strcmp(players[print].firstName, "T") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "U")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strcmp(players[print].firstName, "U") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "V")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strcmp(players[print].firstName, "V") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "W")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strcmp(players[print].firstName, "W") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "X")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strcmp(players[print].firstName, "X") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else if(strcmp(inital, "Y")==0) {

    for (print = 0; print < noOfPlayers; print++) {


    if (strcmp(players[print].firstName, "Y") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }

    else {

    for (print = 0; print < noOfPlayers; print++) {


    if (strcmp(players[print].firstName, "Z") == 0) {

    printf("||%-22s %-6s %3d %9d %7d %7d %7d %7d ||", players[print].familyName, players[print].firstName,
    players[print].playerNo, players[print].noSeasonsPlayed, players[print].noGamesPlayed, players[print].noGoals,
    players[print].worth, players[print].ID);
    printf("||________________________________________ ____________________________________||");
    }
    }
    printf("\n");
    }
    } // end of search by inital


    } // end of list function!



    void outputFile(FILE *PdataR, PlayerInfo players[], int noOfPlayers) {

    /* Runs when the user wishes to exit your program */

    int write = 0;
    printf("HERE1!\n");

    /* loop control, used for sending information to the file */

    if (PdataR == NULL) {
    printf("HERE1!\n");
    /* checks if the file exists */
    printf("Error, this file does not exist!");
    exit(0);
    }

    else {
    /* if it does... */
    printf("HERE2!\n");
    printf("noOfPlayers is: %d", noOfPlayers);
    printf("write is: %d", write);

    while(write < noOfPlayers && !feof(PdataR)) {
    /* Prints the array information to the file */
    printf("HERE1!\n");
    fprintf(PdataR,"%s", players[write].familyName);
    printf("HERE3\n");
    fprintf(PdataR, " ");
    printf("HERE4\n");
    /* This is so there is always a space between the player's first and last name, no matter what the size */
    fprintf(PdataR,"%s\n", players[write].firstName);
    printf("HERE5\n");
    fprintf(PdataR,"%-d %-d %-d %-d %-d %-d\n", players[write].playerNo, players[write].noSeasonsPlayed,
    players[write].noGamesPlayed, players[write].noGoals, players[write].worth, players[write].ID);
    printf("HERE6\n");






    write++;
    printf("write is: %d", write);
    /* updating the array position */


    } // end while
    } // end else
    printf("HERE7\n");
    } // end of outputFile function
    [\CODE]

    and main.c....

    Code:
    #include "functions.h"
    
    int main () {
    
    
    	FILE *PdataR = fopen("players.txt", "r+");
    	FILE *PdataW = fopen("playersTemp.txt", "w");
    
    	PlayerInfo players[2];
    	
    	char *familyNameAdd;								
    	char *firstNameAdd;
    
    	int noOfPlayers;
    
    	familyNameAdd = malloc (sizeof(char) * (STRING_LENGTH) + 1);
    	firstNameAdd = malloc (sizeof(char) * (STRING_LENGTH) + 1);	
    
    	
    	
    	
    
    
    	noOfPlayers = readFilePlayers(PdataR, players);
    	
    	
    
    	printf("%d", noOfPlayers);
    
    	displayPlayerInfoFamily(players, noOfPlayers);
    
    	addPlayer (players, familyNameAdd, firstNameAdd, &noOfPlayers); 
    	display(players, noOfPlayers);
    
    
    
    	outputFile(PdataW, players, noOfPlayers);
    
    
    	
    }
    looking for why it's still outputting to a new file right now...

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    14
    Found that bit....hang on changing now

  11. #11
    Registered User
    Join Date
    Jun 2006
    Posts
    14
    Ok, using "rw" it only leaves what was in the file to begin with, "rw+" as was said by kermit, truncates it so it wipes my first player...

  12. #12
    Registered User
    Join Date
    Jun 2006
    Posts
    14
    Maybe its my outputFile?

  13. #13
    Registered User
    Join Date
    Jun 2006
    Posts
    14
    I am now using a+, but still not adding to the end of file!?

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    903
    You have WAY too much code for what you are doing. And I really mean you have soooooooo much code for so less results.

    Here's an example. I haven't tested it, but this code should behave exactly like you 1000-line display() function. I cut over 900 lines.
    Code:
    #define MAX_SEARCH 255
    
    void display(PlayerInfo players[MAX_PLAYERS], int noOfPlayers) {
    	PlayerInfo** result = new PlayerInfo*[noOfPlayers];
    	char search[MAX_SEARCH + 1];
    	int count_result = 0;
    	int print = 0;
    
    	printf("noOfPlayers: %d\n", noOfPlayers);
    	printf("How do you wish to list the player(s)?\n");
    	printf("Surname, Initial, Seasons, Games, Goals or Worth?");
    	scanf("%s", &search);
    
    	if (strcmp(search, "Seasons") == 0) {
    		int seasons = 0;
    		printf("How many seasons has the player(s) played? ");
    		scanf("%d", &seasons); 
    
    		for (print = 0; print < noOfPlayers; print++) {	
    			if (players[print].noSeasonsPlayed == seasons)
    				result[count_result++] = &players[print];
    	}
    
    	if (strcmp(search, "Games") == 0) {
    		int games = 0;
    		printf("How many games has the player(s) played? ");
    		scanf("%d", &games); 
    
    		for (print = 0; print < noOfPlayers; print++)
    			if (players[print].noGamesPlayed == games)
    				result[count_result++] = &players[print];
    	}
    
    	if (strcmp(search, "Goals") == 0) {
    		int goals = 0;
    		printf("How many goals has the player(s) scored? ");
    		scanf("%d", &goals); 
    
    		for (print = 0; print < noOfPlayers; print++) 
    			if (players[print].noGoals == goals)
    				result[count_result++] = &players[print];
    	}
    
    	if (strcmp(search, "Worth") == 0) {
    		int worth = 0;
    		printf("How much is the player(s) worth? ");
    		scanf("%d", &worth); 
    
    		for (print = 0; print < noOfPlayers; print++)
    			if (players[print].worth == worth)
    				result[count_result++] = &players[print];
    	}
    
    	if (strcmp(search, "Surname") == 0) {
    		char last;
    		printf("Please enter the first letter of their last name: ");
    		scanf("%s", &last);
    		
    		for (print = 0; print < noOfPlayers; print++)
    			if (players[print].familyName[0] == last)
    				result[count_result++] = &players[print];
    	}
    
    	if (strcmp(search, "Initial") == 0) {
    		char initial;
    		printf("Please enter their initial: ");
    		scanf("%s", &inital);
    			
    		for (print = 0; print < noOfPlayers; print++)
    			if (players[print].firstName[0] == initial) 
    				result[count_result++] = &players[print];
    	}
    
    	printf("\n================================================  ================================");
    	printf("||                                          Players                           ||");
    	printf("==================================================  ==============================");
    	printf("||                                                                            ||");
    	printf("||Last                 Inital| PlayerNo  Seasons  Games  noGoals  worth    ID ||");
    	printf("||________________________________________________  ____________________________||");
    
    	for(int i = 0; i < count_result; i++) {
    		printf("||%s %s %d %d %d %d %d %d ||", result[i]->familyName,  result[i]->firstName, 
    			result[i]->playerNo,  result[i]->noSeasonsPlayed,  result[i]->noGamesPlayed,  result[i]->noGoals, 
    			result[i]->worth,  result[i]->ID);
    		printf("||________________________________________________  ____________________________||\n");
    	}
    }
    There is still a lot of place for improvement but I'm quite tired now.
    Last edited by Desolation; 06-25-2006 at 08:20 PM.

  15. #15
    Registered User
    Join Date
    Jun 2006
    Posts
    14
    Yea I know my code is huge, but I'm not worrying about that right now....just trying to get things 2 work, will deal with it later. But, I have got it to save to the same file....but now in my team struct, I have an array of numbers which correspond to the ID numbers of the players in the team. I am wondering how to print those numbers stored in the array, so they can be used for when a player scores a goal and has their goal count increased, their worth increased etc...

    Code:
    void displayTeamPlayers(TeamInfo teams[MAX_TEAMS], int noOfTeams) {
    
    int print = 0;
    	int test;
    
    	char teamNameIn[STRING_LENGTH];
    
    	printf("Please enter the team: ");
    	scanf("%s", teamNameIn);
    	
    	for (print = 0; print < noOfTeams; print++) {	
    
    		if (strcmp(teams[print].teamName,teamNameIn)==0) {
    
    	ID1 = teams[print].player_array[0];
    			ID2 = teams[print].player_array[1];
    			ID3 = teams[print].player_array[2];
    			ID4 = teams[print].player_array[3];
    			ID5 = teams[print].player_array[4];
    			ID6 = teams[print].player_array[5];
    			ID7 = teams[print].player_array[6];
    			ID8 = teams[print].player_array[7];
    			ID9 = teams[print].player_array[8];
    			ID10 = teams[print].player_array[9];
    			ID11 = teams[print].player_array[10];  /*why can't I do this?*/
    
    printf("Team players \n");
    			printf("==================================================\n");
    			
    			printf("||Player1ID: %d \n", teams[print].player_array[0]);
    			//printf("%d\n", ID1);
    			printf("||Player2ID: %d \n", teams[print].player_array[1]);
    			//printf("%d\n", ID2);
    			printf("||Player3ID: %d \n", teams[print].player_array[2]);
    			//printf("%d\n", ID3);
    			printf("||Player4ID: %d \n", teams[print].player_array[3]);
    			//printf("%d\n", ID4);
    			printf("||Player5ID: %d \n", teams[print].player_array[4]);
    			//printf("%d\n", ID5);
    			printf("||Player6ID: %d \n", teams[print].player_array[5]);
    			//printf("%d\n", ID6);
    			printf("||Player7ID: %d \n", teams[print].player_array[6]);
    			//printf("%d\n", ID7);
    			printf("||Player8ID: %d \n", teams[print].player_array[7]);
    			//printf("%d\n", ID8);
    			printf("||Player9ID: %d \n", teams[print].player_array[8]);
    			//printf("%d\n", ID9);
    			printf("||Player10ID: %d \n", teams[print].player_array[9]);
    			//printf("%d\n", ID10);
    			printf("||Player11ID: %d \n", teams[print].player_array[10]);
    			//printf("%d\n", ID11);
    			printf("||________________________________________________\n");
    		}
    	}
    }
    any ideas? Do I have to use pointers?

    Cheers,

    Submeg.
    Last edited by Submeg; 06-25-2006 at 11:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Help on making program run more efficiently
    By peeweearies in forum C Programming
    Replies: 2
    Last Post: 03-23-2009, 02:01 AM
  3. Making a program take a variable number of arguments
    By subtled in forum C Programming
    Replies: 1
    Last Post: 04-17-2007, 05:38 AM
  4. Making interest rate program in C
    By canadas321 in forum C Programming
    Replies: 6
    Last Post: 06-23-2005, 11:59 AM