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: insertnamehere8 is an unknown quantity at this point 
Solved Threads: 0
insertnamehere8 insertnamehere8 is offline Offline
Newbie Poster

Searching for a match in a 2D array.

 
0
  #1
Nov 17th, 2008
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #2
Nov 17th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

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

 
0
  #3
Nov 17th, 2008
Originally Posted by Ancient Dragon View Post
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()
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 2
Reputation: insertnamehere8 is an unknown quantity at this point 
Solved Threads: 0
insertnamehere8 insertnamehere8 is offline Offline
Newbie Poster

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

 
0
  #4
Nov 17th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC