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 == 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.

Recommended Answers

All 3 Replies

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().

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 <cstring> to use strcmp()

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. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.