954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Searching for a match in a 2D array.

Hi. This is for a phonebook assignment. Part of the assignment involves searching a 2D array. This is the function that reads the phonebook file.

void getData(char name[][NAMESIZE], char telephone[][PHONESIZE],
     char zipcode[][ZIPSIZE], char address[][ADDSIZE], int& size);

~~~~~

void getData(char name[][NAMESIZE], char telephone[][PHONESIZE],
     char zipcode[][ZIPSIZE], char address[][ADDSIZE], int& size){
    int i = 0;
    ifstream infile("phone_dir.txt");
	while(!infile.eof()) {                                        
        infile.getline(name[i],      NAMESIZE,  '\t');                     
        infile.getline(telephone[i], PHONESIZE, '\t');                     
        infile.getline(address[i],   ADDSIZE,   '\t');               
        infile.getline(zipcode[i],   ZIPSIZE,   '\n');
		i++;
    }
}


I'm stuck in that no matter what I do I can't seem to find the match in the array?

Say I'm looking for the name Jack Daniel. I type in Jack and can't get a response. The only thing I tried was something like name[i] == search I know this isn't how the search is to be done, that part I can figure out myself. It's just that no matter what I can't figure out how to get a positive from searching for "Jack" when the name array contains "Jack Daniel". At a complete loss, help would be appreciated.

insertnamehere8
Newbie Poster
2 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

when using character arrays the == operator does not compare string content but string addresses, so it will never work. You need to call strcmp() which returns 0 if the two strings are exactly the same. if( strcmp(name[i], "Jack Danial" == 0) . strcmp() is case sensitive, meaning "Jack" is not the same as "JACK". Many compilers have case-insensive compares, such as stricmp() or comparenocase().

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
when using character arrays the == operator does not compare string content but string addresses, so it will never work. You need to call strcmp() which returns 0 if the two strings are exactly the same. if( strcmp(name[i], "Jack Danial" == 0) . strcmp() is case sensitive, meaning "Jack" is not the same as "JACK". Many compilers have case-insensive compares, such as stricmp() or comparenocase().

Don't forget to#include to use strcmp()

minas1
Junior Poster in Training
82 posts since Nov 2008
Reputation Points: 13
Solved Threads: 8
 

Oh boy, I think I've just made a fool of myself. Figures I missed the most simple thing in the program. Everything works perfectly now. What I get for banging my head against this in the midst of the night. Thank you very much guys. :)

insertnamehere8
Newbie Poster
2 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You