Thread: Different string comparison question

  1. #1
    Registered User P3nGu1N's Avatar
    Join Date
    Feb 2009
    Location
    La Crescent, MN
    Posts
    23

    Different string comparison question

    I cannot seem to match up the strings when looking for them in my SymTab. I know the hash function works and will always hash the name to the correct location, so it must be something semantic I'm doing when trying to compare the strings. Please help.

    Code:
    struct SymEntry* FindName(struct SymTab *ATable,
                               const char * Name) {
    									
    		printf("\nFinding Name: \"%s\" ", Name);
    		int comp;
    		int hashValue = hash(Name,ATable);
    
    		char n[200];
    		int nameLength;
    		struct SymEntry *currentEntry;
    		
    		strcpy(n, Name);
    		nameLength = strlen(n) + 1;
    		
    		if(ATable->Contents[hashValue] == NULL){
    			printf("\nATable->Contents at hashValue were NULL, returning NULL\n");
    			return NULL;
    		} /*else */
    		for(currentEntry = ATable->Contents[hashValue]; 
    			currentEntry->Next != NULL; currentEntry = currentEntry->Next) {
    			printf("\ncurrentEntry = %s\n",currentEntry->Name);
    			printf("\ncomparing n = %s to currentEntry = %s\n", n , currentEntry->Name);  
    			comp = strcmp( n, currentEntry->Name);
    			printf("\ncomp = %d",comp);
    			if (comp == 0){
    				return currentEntry;
    			}
    		}
    		return NULL; /* wasn't found in the list at the hash value */
    };
    the printf's are just for debugging, sorry if it makes a more jumbled code.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if currentEntry->Next == NULL
    you do not enter the loop, and do not check the name,

    replace the for loop condition to check currentEntry != NULL
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User P3nGu1N's Avatar
    Join Date
    Feb 2009
    Location
    La Crescent, MN
    Posts
    23
    good call, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM