code condensing

This is a discussion on code condensing within the C++ Programming forums, part of the General Programming Boards category; I'm pretty new to programming, and all of this code is linear... I'm sure you all will take a look ...

  1. #1
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69

    code condensing

    I'm pretty new to programming, and all of this code is linear... I'm sure you all will take a look at it and giggle somewhat... but I am curious as to if there is a way to condense these "arrays" that i have for "big10" and "pac10" into something that is easiier to read and write, instead of writing 10 or 11 seperate commands for each value in the array. here is the code... I am most interested in seeing how to use maybe a vector or placing them into an array of some sort...

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    
    struct initInfo
    {
    	char number [5];
    	char lastName [20];
    	char firstName [20];
    	char position [5];
    	char yearsExp [5];
    	char college [20];
    	char nflTeam [30];
    
    };
    
    struct pac10
    {
    	char firstName [20];
    	char lastName [20];
    	char yearsExp [5];
    	char college [30];
    	char nflTeam [50];
    };
    
    struct big10
    {
    	char firstName [20];
    	char lastName [20];
    	char yearsExp [5];
    	char college [30];
    	char nflTeam [50];
    };
    
    int main()
    {
    
    
    	fstream file_op("pac10.txt", ios::out);
    	fstream file_op1("big10.txt", ios::out);
    
    
    	ifstream playersFile;;
    	char *stopString;
    
    	initInfo info;
    	pac10 infoPac10;
    	big10 infoBig10;
    
    	int option;
    	int i;
    	int j = 0;
    	
    	
    	char pac101 [] = "Arizona";
    	char pac102 [] = "Arizona State";
    	char pac103 [] = "California";
    	char pac104 [] = "Oregon";
    	char pac105 [] = "Oregon State";
    	char pac106 [] = "Southern California";
    	char pac107 [] = "Stanford";
    	char pac108 [] = "UCLA";
    	char pac109 [] = "Washington";
    	char pac1010 [] = "Washington State"; 
    
    	char big101 [] = "Illinois";
    	char big102 [] = "Indiana";
    	char big103 [] = "Iowa";
    	char big104 [] = "Michigan";
    	char big105 [] = "Michigan State";
    	char big106 [] = "Minnesota";	
    	char big107 [] = "Northwestern";	
    	char big108 [] = "Ohio State";	
    	char big109 [] = "Penn State";
    	char big1010 [] = "Purdue";
    	char big1011 [] = "Wisconsin";
    
    
    	playersFile.open("players.txt");
    
    	if (playersFile.fail())
    	{
    		cout<<"An error occured while trying to open the file."<<endl;
    		system ("PAUSE");
    		exit (1);
    	}
    
    	cout<<"File opened"<<endl;
    	
    	while (!playersFile.eof())
    	{
    
    		playersFile.getline(info.number, 11, ',');
    		playersFile.getline(info.lastName, 20,  ',');
    		playersFile.getline(info.firstName, 20, ',');
    		playersFile.getline(info.position, 20, ',');
    		playersFile.getline(info.yearsExp, 5, ',');
    		playersFile.getline(info.college, 50, ',');
    		playersFile.getline(info.nflTeam, 40);
    		
    /*
    		cout<<"number is: "<<info.number<<endl;
    		cout<<"last name is: "<<info.lastName<<endl;
    		cout<<"first name is: "<<info.firstName<<endl;
    		cout<<"position is: "<<info.position<<endl;
    		cout<<"years exp: "<<info.yearsExp<<endl;
    		cout<<"college is: "<<info.college<<endl;
    		cout<<"nfl is: "<<info.nflTeam<<endl;
    */
    		
    		//compares for PAC10 colleges, if they go to a pac10 school, writes to a file
    		if (strcmp(pac101, info.college)==0)
    		{		
    			file_op<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;	
    		}
    		if (strcmp(pac102, info.college) == 0)
    		{
    			file_op<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;	
    		}
    		if (strcmp(pac103, info.college) == 0)
    		{
    			file_op<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;	
    		}
    		if (strcmp(pac104, info.college) == 0)
    		{
    			file_op<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;	
    		}
    		if (strcmp(pac105, info.college) == 0)
    		{
    			file_op<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;	
    		}
    		if (strcmp(pac106, info.college) == 0)
    		{
    			file_op<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;	
    		}
    		if (strcmp(pac107, info.college) == 0)
    		{
    			file_op<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;	
    		}
    		if (strcmp(pac108, info.college) == 0)
    		{
    			file_op<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;	
    		}
    		if (strcmp(pac109, info.college) == 0)
    		{
    			file_op<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;	
    		}
    		if (strcmp(pac1010, info.college) == 0)
    		{
    			file_op<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;	
    		}
    
    		//compares for big10 colleges, if they go to a big10 school, writes info down to a file
    		if (strcmp(big101, info.college) == 0)
    		{
    			file_op1<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;
    		}
    		if (strcmp(big102, info.college) == 0)
    		{
    			file_op1<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;
    		}
    		if (strcmp(big103, info.college) == 0)
    		{
    			file_op1<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;
    		}
    		if (strcmp(big104, info.college) == 0)
    		{
    			file_op1<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;
    		}
    		if (strcmp(big105, info.college) == 0)
    		{
    			file_op1<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;
    		}
    		if (strcmp(big106, info.college) == 0)
    		{
    			file_op1<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;
    		}
    		if (strcmp(big107, info.college) == 0)
    		{
    			file_op1<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;
    		}
    		if (strcmp(big108, info.college) == 0)
    		{
    			file_op1<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;
    		}
    		if (strcmp(big109, info.college) == 0)
    		{
    			file_op1<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;
    		}
    		if (strcmp(big1010, info.college) == 0)
    		{
    			file_op1<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;
    		}
    		if (strcmp(big1011, info.college) == 0)
    		{
    			file_op1<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;
    		}
    
    
    
    		i = strtol(info.yearsExp, &stopString, 10); 
    
    		if (i >= 5)
    		{
    			j++;
    
    		}
    
    
    		
    	}
    	playersFile.close();
    	file_op.close();
    	file_op1.close();
    	
    cout<<"Enter an option number between 1 and 7: ";
    cin>>option;
    
    while (option > 7)
    {
    	cout<<"Option must be lower than 7 dumbass...";
    	cout<<"Try it again... Enter an option between 1 and 7: ";
    	cin>>option;
    }
    
    while (option < 1)
    {
    	cout<<"There is no option lower than 0 dumbass..."<<endl;
    	cout<<"Try it again... Enter an option between 1 and 7: "<<endl;
    	cin>>option;
    }
    
    
    if (option = 2)
    {
    	file_op.open("pac10.txt");
    
    	while (!file_op.eof())
    	{
    
    	file_op.getline(infoPac10.firstName, 50, ',');
    	file_op.getline(infoPac10.lastName, 50, ',');
    	file_op.getline(infoPac10.yearsExp, 10, ',');
    	file_op.getline(infoPac10.college, 50, ',');
    	file_op.getline(infoPac10.nflTeam, 50);
    
    	cout<<infoPac10.firstName<<infoPac10.lastName<<", "<<infoPac10.yearsExp<<", "<<infoPac10.college<<endl;
    
    	}
    	file_op.close();
    }
    
    if (option = 5)
    {
    	file_op1.open("big10.txt");
    	
    	while (!file_op1.eof())
    	{
    		file_op1.getline(infoBig10.firstName, 50, ',');
    		file_op1.getline(infoBig10.lastName, 50, ',');
    		file_op1.getline(infoBig10.yearsExp, 10, ',');
    		file_op1.getline(infoBig10.college, 50, ',');
    		file_op1.getline(infoBig10.nflTeam, 50);
    
    		cout<<infoBig10.firstName<<infoBig10.lastName<<", "<<infoBig10.yearsExp<<", "<<infoBig10.college<<endl;
    	}
    	file_op1.close();
    }
    
    cout<<"Number of players with 5 more years of exp. are:  "<<j<<endl;
    
    
    return 0;
    
    }


    thanks guys, remember, DON'T LAUGH TOO HARD!!

  2. #2
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    Umm, I'm not quite sure what you're doing there.

    First of all, you're in C++, use C++, that means std::string, which you've even #included.

    So use string instead of char arrays.

    For arrays, you'd use them like this (also with string)
    Code:
    string pac10[10];
    pac10[0] = "Arizona";
    pac10[1] = "Arizona State";
    ...
    The real space saver comes when you're checking through each one, like in here
    Code:
    if (strcmp(pac101, info.college)==0)
    		{		
    			file_op<<info.firstName<<...<<endl;	
    		}
    You'd do something like
    Code:
    for(int n = 0;n<10;n++)
    {
        if(pac10[n] == info.college)
            file_op<<info.firstName<<...
    }
    Last edited by Dweia; 09-07-2005 at 07:41 AM.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    to use a vector of STL::strings instead of an array of STL::strings you would do something like this:


    std::vector<string> pac10;

    pac10.push_back("Arizona");
    pac10.push_back("Southern California");
    //etc.

    The vector could then be accessed and used similar to an array using the [] operator or, depending on your needs, you could use iterators to access individual elements of the vector, which is what a lot of the methods in the vector class and the std algorithms do.
    A vector of char pointers or char arrays should also be possible, but the STL classes are designed to work with one another/switch between one another with a minimum of hassle, so using char pointers or char arrays with vectors seems a little pointless, unless you are required to do so.

    In this case, the advantage of vectors over arrays is not the automatic enlargement when needed, but the ability to use standard methods and algorithms to manipulate/search the items therein so you don't have to do it from scratch. Likewise the advantage of STL strings over null terminated char arrays is so you can use the built in == operator rather than the somewhat convoluted strcmp function to compare the strings.

    BTW: I would encourage you to get into the habit of not using eof() as the terminating condition for loops as in the following lines:
    Code:
    while (!playersFile.eof())
    {
      playersFile.getline(info.number, 11, ',');
    Instead, use the second line above as the conditional of the while loop. Search the board or look at the FAQ if you want to learn why.
    You're only born perfect.

  4. #4
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    When i do this code, it gives me multiple entries of the same data... for instance, in the "big10.txt" file, after the program is run with this code now in it...

    Code:
    for (int n=0; n<10; n++)
    		{
    			if (pac10[n]==info.college)
    				file_op<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;
    		}

    it is giving me this in my text file...

    Code:
     Matt , Katula,0,Wisconsin,Baltimore Ravens
     Matt , Katula,0,Wisconsin,Baltimore Ravens
     Matt , Katula,0,Wisconsin,Baltimore Ravens
     Matt , Katula,0,Wisconsin,Baltimore Ravens
     Matt , Katula,0,Wisconsin,Baltimore Ravens
     Matt , Katula,0,Wisconsin,Baltimore Ravens
     Matt , Katula,0,Wisconsin,Baltimore Ravens
     Matt , Katula,0,Wisconsin,Baltimore Ravens
     Matt , Katula,0,Wisconsin,Baltimore Ravens
     Matt , Katula,0,Wisconsin,Baltimore Ravens
     Matt , Katula,0,Wisconsin,Baltimore Ravens
     Lee , Evans,2,Wisconsin,Buffalo Bills
     Lee , Evans,2,Wisconsin,Buffalo Bills
     Lee , Evans,2,Wisconsin,Buffalo Bills
     Lee , Evans,2,Wisconsin,Buffalo Bills
     Lee , Evans,2,Wisconsin,Buffalo Bills
     Lee , Evans,2,Wisconsin,Buffalo Bills
     Lee , Evans,2,Wisconsin,Buffalo Bills
     Lee , Evans,2,Wisconsin,Buffalo Bills
     Lee , Evans,2,Wisconsin,Buffalo Bills
     Lee , Evans,2,Wisconsin,Buffalo Bills
     Lee , Evans,2,Wisconsin,Buffalo Bills

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    It looks like you probably have a nested loop somewhere where you don't need one. The code could I would use would probably look like this in pseudo code:
    Code:
    while(read in first line of player data)
       read in rest of player data
       display player data
     
       //set a flag each time through to false
       shool found equals false
     
       //look for players school among the pac10 and big10 schools
       for each pac10 school
    	 if pac10 shool name equal college name of player
    		write player data to pac10 file
    		school found equal true
    		break;
     
       //if school not found in pac10 look in big10
       if not school found
    	 for each big10 school
    	   if big10 school name equal college name of player
    		  write player data to big10 file
    		  break
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21