Thread: Personal Program that is making me go wtf?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

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