Kindly help me...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 7
Reputation: kneel is an unknown quantity at this point 
Solved Threads: 0
kneel kneel is offline Offline
Newbie Poster

Kindly help me...

 
0
  #1
Jul 31st, 2008
how can the searching code be created?
i've made a program named address book. now i wanna make a function that can search an existing contact details from the file through 2 ways
1) search by name
2) search by phone No.
kindly define me the code in C++
thx
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
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: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Kindly help me...

 
0
  #2
Jul 31st, 2008
The simplest, but slowest, way is to do a linear search -- start from the beginning of the file, read each record until you get the one you want. For files written in text mode there isn't much more that can be done to speed up the searches.

A more complicated method is to create an index file that contains key field values and the index value into the data file.
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: Mar 2008
Posts: 1,400
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 113
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Kindly help me...

 
0
  #3
Jul 31st, 2008
If you want to find a name and display the telephone number you have to loop through each person in the address book and then use the strcmp function which takes two char pointers as parameters and returns 0 if they match.

It would look something similar to this:
  1. char *nameToFind = "John";
  2.  
  3. for (int i = 0; i < peopleCount; ++i) {
  4. if (strcmp(names[i], nameToFind) == 0) {
  5. std::cout << phoneNo[i];
  6. }
  7. }

The only problem with this is that if you want to search for part of a name, the code above wont work. So in order for the program to return "John Smith´s" number by just typing the name "John", you need a seperate function that checks to see whether "John Smith" contains the word "John".
For this try looking up the strstr function.

Hope this helps.
Last edited by William Hemsworth; Jul 31st, 2008 at 4:47 pm.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Kindly help me...

 
0
  #4
Aug 1st, 2008
Originally Posted by Ancient Dragon View Post
A more complicated method is to create an index file that contains key field values and the index value into the data file.
Sounds almost like SQL O_O
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
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: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Kindly help me...

 
0
  #5
Aug 1st, 2008
Originally Posted by Alex Edwards View Post
Sounds almost like SQL O_O
Its not nearly as complex as SQL or relational databases.
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  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC