Loop Optimization & String Comparison.

This is a discussion on Loop Optimization & String Comparison. within the C++ Programming forums, part of the General Programming Boards category; This is incredibly messy code. It works, I can tell ya'll that, but it's messy and I believe that there ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313

    Loop Optimization & String Comparison.

    This is incredibly messy code. It works, I can tell ya'll that, but it's messy and I believe that there as to be a way to optimize this in some sort of loop.

    I had the idea of throwing all of the strcmp()'s into a for loop, but wouldn't it still have to do all of the comparisons if it did? And I wasn't sure how to work a dynamic strcmp in a while loop, though my one attempt (while(strcmp(exits, DYN_WORD))) failed miserably.

    Anyone here who can help me figure out how to even start optimizing this mess?

    Thank you in advance.



    Edit: The reason there is a space between the letter(s) and the end quote in the strcmp is because my program grabs that space and appends it to the 'exits' string. I can't figure out how to fix that problem, so I just added the space to the comparison.



    Code:
    void LoadRoom(char* filename)
    {
    	char buffer, exits[8], eflag[8], enemy[64], name[64];
    	char text[64] = "text/";
    
    	strcat(text, filename); // Prepends "text/" to the filename so it searches the text directory.
    
    	std::fstream file(text);
    		if (!file)
    		{
    			std::cout << "Error! The game has encountered an unrecoverable error.\n";
    			std::cout << "We apoligize for the inconvenience.\n";
    			std::cout << "The game shall now exit.\n\n";
    
    			errflag = 1;
    			quitflag = 1;
    		}
    
    		// The getlines will get info, get the garbage, then get more info until all the information has been gathered.	
    		file.getline(name, 64, '|');
    		file.get(buffer);
    
    		file.getline(exits, 8, '|');
    		file.get(buffer);
    
    		file.getline(north, 16, '|');
    		file.get(buffer);
    
    		file.getline(south, 16, '|');
    		file.get(buffer);
    
    		file.getline(east, 16, '|');
    		file.get(buffer);
    
    		file.getline(west, 16, '|');
    
    		file.getline(eflag, 8, '|');
    		file.get(buffer);
    
    		file.getline(eflag, 8, '|');
    		file.get(buffer);
    
    		file.getline(enemy, 64);
    
    		if (strcmp(exits, "NSEW ") == 0)
    		{
    			n = 1;
    			s = 1;
    			e = 1;
    			w = 1;
    		}
    
    		if (strcmp(exits, "NSE ") == 0)
    		{
    			n = 1;
    			s = 1;
    			e = 1;
    			w = 0;
    		}
    
    		if (strcmp(exits, "NSW ") == 0)
    		{
    			n = 1;
    			s = 1;
    			e = 0;
    			w = 1;
    		}
    
    		if (strcmp(exits, "NEW ") == 0)
    		{
    			n = 1;
    			s = 0;
    			e = 1;
    			w = 1;
    		}
    
    		if (strcmp(exits, "SEW ") == 0)
    		{
    			n = 0;
    			s = 1;
    			e = 1;
    			w = 1;
    		}
    
    		if (strcmp(exits, "NS ") == 0)
    		{
    			n = 1;
    			s = 1;
    			e = 0;
    			w = 0;
    		}
    
    		if (strcmp(exits, "NE ") == 0)
    		{
    			n = 1;
    			s = 0;
    			e = 1;
    			w = 0;
    		}
    
    		if (strcmp(exits, "NW ") == 0)
    		{
    			n = 1;
    			s = 0;
    			e = 0;
    			w = 1;
    		}
    
    		if (strcmp(exits, "SE ") == 0)
    		{
    			n = 0;
    			s = 1;
    			e = 1;
    			w = 0;
    		}
    
    		if (strcmp(exits, "SW ") == 0)
    		{
    			n = 0;
    			s = 1;
    			e = 0;
    			w = 1;
    		}
    
    		if (strcmp(exits, "EW ") == 0)
    		{
    			n = 0;
    			s = 0;
    			e = 1;
    			w = 1;
    		}
    
    		if (strcmp(exits, "N ") == 0)
    		{
    			n = 1;
    			s = 0;
    			e = 0;
    			w = 0;
    		}
    
    		if (strcmp(exits, "S ") == 0)
    		{
    			n = 0;
    			s = 1;
    			e = 0;
    			w = 0;
    		}
    
    		if (strcmp(exits, "E ") == 0)
    		{
    			n = 0;
    			s = 0;
    			e = 1;
    			w = 0;
    		}
    
    		if (strcmp(exits, "W ") == 0)
    		{
    			n = 0;
    			s = 0;
    			e = 0;
    			w = 1;
    		}
    
    		if (strcmp(eflag, "T ") == 0) // Annoyingly broken.
    		{
    			std::fstream file("temp/erlist.txt");
    				file.seekg(std::ios::end);
    				file << filename << " | true";
    			file.close();
    		} // Figure out how to fix this loop someday.
    
    		while (file.get(buffer) != NULL) // While not at the end of the file.
    		{
    			std::cout << buffer;
    		}
    	file.close();
    }
    Last edited by Lithorien; 08-09-2004 at 04:13 PM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,648
    Here's something to chew on.
    Code:
    void get_directions(const char *dir)
    {
        n = s = e = w = 0;
    
        while (*dir != '\0')
        {
            switch (*dir)
            {
                case 'N':
                    n = 1;
                    break;
    
                case 'S':
                    s = 1;
                    break;
    
                case 'E':
                    e = 1;
                    break;
    
                case 'W':
                    w = 1;
                    break;
            }//switch
    
            dir++;
        }//while
    }//get_directions
    gg

  3. #3
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by Codeplug
    Here's something to chew on.

    gg
    You are a god.

    Code:
    n = s = e = w = 0;
    
    for (int x = 0; x < 5; x++)
    {
    	if (exits[x] == 'N')
    	{
    		n = 1;
    	}
    	if (exits[x] == 'S')
    	{
    		s = 1;
    	}
    	if (exits[x] == 'E')
    	{
    		e = 1;
    	}
    	if (exits[x] == 'W')
    	{
    		w = 1;
    	}
    }
    Thank you, thank you, thank you, thank you, thank you!

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,648
    You need to stop when you reach the null terminator.
    And use "else if"'s.

    gg

  5. #5
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by Codeplug
    You need to stop when you reach the null terminator.
    I thought that that was what I was doing with the for loop. Only searching the first four elements (where N, S, E, and W would be) and ignoring the rest.

    Quote Originally Posted by Codeplug
    And use "else if"'s.

    gg
    Done. Thanks for the catch.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,648
    The loop assumes there will always be 5 valid characters in the exits array, indexes 0 through 4.

    "S " <-- 3 valid indexes, 0='S', 1=' ', 2='\0'

    gg

  7. #7
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by Codeplug
    The loop assumes there will always be 5 valid characters in the exits array, indexes 0 through 4.

    "S " <-- 3 valid indexes, 0='S', 1=' ', 2='\0'

    gg
    OH. Very good point. *blink*

    Code:
    for (int x = 0; x < 5; x++)
    {
    	if (exits[x] == '\0')
    	{
    		x = 5;
    	}
    	else if (exits[x] == 'N')
    	{
    		// ...

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,648
    Or
    Code:
    for (int x = 0; exits[x] != '\0'; x++)
    gg

  9. #9
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by Codeplug
    Or
    Code:
    for (int x = 0; exits[x] != '\0'; x++)
    gg
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  2. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 07:26 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  4. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM

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