Question

c++ programming language Im pretty lost when it comes to figuring out how to search though...

c++ programming language Im pretty lost when it comes to figuring out how to search though to find the phone number for the correct name entered. The rest makes sense to me if someone could please help me with the area mentioned I would be very greateful  thank you.

The following code ceates a small phone book. An array is used to store a list of names and another array is used to store the phone numbers that go with each name. For example, Michael Myers’ phone number is 333- 8000 and Ash Williams’ phone number is 333-2323. Write the function lookupName so the code properly looks up and returns the phone number for the input target name.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question, here is how you can implement the lookup function. You need to maintain 2 parallel arrays, one for names and one for phone numbers. At each index, it will store the name and his/her phone number.

Then you need to write a function passing all the details - names[] array, phone_numbers array[] , search name, and array size. it will first try to search the name in the array and if the name matches returns the index

if name not found it will return -1 indicating there is no name in the array

then interpreting the index value will print the phone number that exists at that index corresponding to the person

Here is the code you can check,

#include<string>
#include<iostream>

using namespace std;


int lookupName(const string names[], const string phone_numbers[], string lookup, int size){
  
   for(int i=0; i<size;i++){
       if(names[i].compare(lookup)==0){
           return i;
       }
   }
   return -1;
}


int main(){
  
   string names[] ={"Michael Myer","Ash Williams","John Abraham","Tony Blair","Donald Trump"};
   string phone_numbers[] ={"333-8000","333-2323","456-7890","111-9999","234-6789"};
  
   string lookup_name;
   cout<<"Enter the name for whose phone number you want to search: ";
   getline(cin,lookup_name);
  
   int index = lookupName(names,phone_numbers,lookup_name,5);
   if(index>=0)
   cout<<names[index]<<"\'s phone number is "<<phone_numbers[index]<<endl;
   else
   cout<<"No record found for name: "<<lookup_name<<endl;
  
}

==========================================================================================

let me know for any help, and please do give a thumbs up : )

thanks a lot!

Add a comment
Know the answer?
Add Answer to:
c++ programming language Im pretty lost when it comes to figuring out how to search though...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT