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.

Dani AI

Generated

Short answer: to have a search for "Jack" match a stored "Jack Daniel" you need substring/prefix matching (and usually case-insensitive comparison) rather than an exact-equality check. Below is a compact, practical approach that (a) reads the tab-separated file robustly, (b) normalizes case, and (c) tests for the query as a substring of the name.

// case-insensitive substring test
#include <algorithm>
#include <string>
#include <sstream>
#include <fstream>
#include <cctype>

bool ci_contains(std::string hay, std::string needle){
    auto tolowerb = [](unsigned char c){ return static_cast<char>(std::tolower(c)); };
    std::transform(hay.begin(), hay.end(), hay.begin(), tolowerb);
    std::transform(needle.begin(), needle.end(), needle.begin(), tolowerb);
    return hay.find(needle) != std::string::npos;
}

// example: parse phone_dir.txt (tab-separated) and match query against name
std::ifstream in("phone_dir.txt");
std::string line;
while(std::getline(in, line)){
    std::istringstream ss(line);
    std::string name, phone, address, zip;
    std::getline(ss, name, '\t');
    std::getline(ss, phone, '\t');
    std::getline(ss, address, '\t');
    std::getline(ss, zip);
    if(!name.empty() && name.back() == '\r') name.pop_back(); // handle CRLF files
    if(ci_contains(name, query)) { /* match found */ }
}

Troubleshooting notes: remove hidden characters (CR, extra tabs/spaces) if a match fails; printing bytes in hex helps find nonprintables. Bound array capacity or prefer std::vector/std::string to avoid overflow. Prefer the pattern above over while(!infile.eof()) so you only process successful reads.

Thanks to and for the pointer about string-comparison issues and headers; if you switch to the std::string-based approach above (or, for C-style strings, use a substring routine like strstr), typing "Jack" will reliably match "Jack Daniel".

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.