Thread: LL Search Function

  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,817
    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==.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    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, 08:18 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM