LL Search Function

This is a discussion on LL Search Function within the C++ Programming forums, part of the General Programming Boards category; I'm writing a simple LL search function, which will search through the linked list for a Name. If found, it ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    54

    LL Search Function

    I'm writing a simple LL search function, which will search through the linked list for a Name. If found, it will print out the Name and ID associated with the name, and continue searching.

    I stepped through the function in VStudio and it recognizes what it is comparing and the input, but it is not saying they are equal for some reason, and I cannot figure out why.

    Code:
    void LList::search(char Name[]) {
    	int num_found=0;
    	People * temp;//allocate memory for struct
    	temp = first;//fill struct with contents of first struct in list
    	if (entries == 0) {
    		cout << "List is empty, nothing to search!\n";
    		return;
    	}//if
    	cout << "Search Results for "<< Name << ":\n";
    	while (temp != NULL) {
    		if (temp->Name == Name) {//if name found
    			cout << temp->Name << " " << temp->ID << endl;
    			num_found++;
    		}//if
    		temp = temp->Next;//traverse the list
    	}//while
    	cout << "Found " << num_found << " entries.\n";
    }//search
    I've been testing things with simple statements:
    list.add_entry("A", 1);
    list.search("A");

    My add_entry, print_list, delete_entry functions all seem to work fine, but this simple one has something going on I am having trouble finding. Any ideas?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,674
    Code:
    void LList::search(char Name[]) {
    	int num_found=0;
    	People * temp;//allocate memory for struct
    	temp = first;//fill struct with contents of first struct in list
    	if (entries == 0) {
    		cout << "List is empty, nothing to search!\n";
    		return;
    	}//if
    	cout << "Search Results for "<< Name << ":\n";
    	while (temp != NULL) {
    		if (temp->Name == Name) {//if name found
    			cout << temp->Name << " " << temp->ID << endl;
    			num_found++;
    		}//if
    		temp = temp->Next;//traverse the list
    	}//while
    	cout << "Found " << num_found << " entries.\n";
    }//search
    You can't compare c-style strings (null terminated char arrays) using operator==. You need to use strcmp. You probably would benefit from using std::string containers instead which would support the use of operator==.
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    3,025
    You're comparing pointers, not the data that is "pointed to".

    Soma

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    Hah duh, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Attempt to write function search()
    By elsewhere in forum C Programming
    Replies: 6
    Last Post: 12-03-2008, 07:18 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM

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