| | |
Searching for a match in a 2D array.
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
C++ Syntax (Toggle Plain Text)
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.
Last edited by insertnamehere8; Nov 17th, 2008 at 3:38 am.
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 PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
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(). ![]() |
Similar Threads
- C++ Random Number Generator (C++)
- searching problems (Perl)
- Search 2Dim array (Java)
- Open Gl Programing Error (help)... (Game Development)
- Source Code that don't work? (Java)
- Reducing if statements..again (C)
- Searching problem (C++)
- Desiging a set of rules for a match (C++)
- Searching in documents... (C++)
- searching multiple fields (PHP)
Other Threads in the C++ Forum
- Previous Thread: Py Lame????
- Next Thread: Problem with OOP and classes...
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






