943,917 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 933
  • C++ RSS
Nov 17th, 2008
0

Searching for a match in a 2D array.

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. void getData(char name[][NAMESIZE], char telephone[][PHONESIZE],
  2. char zipcode[][ZIPSIZE], char address[][ADDSIZE], int& size);
  3.  
  4. ~~~~~
  5.  
  6. void getData(char name[][NAMESIZE], char telephone[][PHONESIZE],
  7. char zipcode[][ZIPSIZE], char address[][ADDSIZE], int& size){
  8. int i = 0;
  9. ifstream infile("phone_dir.txt");
  10. while(!infile.eof()) {
  11. infile.getline(name[i], NAMESIZE, '\t');
  12. infile.getline(telephone[i], PHONESIZE, '\t');
  13. infile.getline(address[i], ADDSIZE, '\t');
  14. infile.getline(zipcode[i], ZIPSIZE, '\n');
  15. i++;
  16. }
  17. }

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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
insertnamehere8 is offline Offline
2 posts
since Nov 2008
Nov 17th, 2008
0

Re: Searching for a match in a 2D array.

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().
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Nov 17th, 2008
0

Re: Searching for a match in a 2D array.

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()
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Nov 17th, 2008
0

Re: Searching for a match in a 2D array.

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.
Last edited by insertnamehere8; Nov 17th, 2008 at 10:07 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
insertnamehere8 is offline Offline
2 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Py Lame????
Next Thread in C++ Forum Timeline: Problem with OOP and classes...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC