c++ help (easy question)

Soldato
Joined
1 Dec 2004
Posts
22,551
Location
S.Wales
Im trying to create a programme which will hold the information of names and phone numbers in an array (either seperate arrays or two dimentional).

When the programme is run i want the user to be able to input a name, in which when the name is inputted, it will search through an array of names, if it finds the name it will output the name (and postition of name in that list) + phone number..if it dont find a name it will simply say name is not found..


Code:
//Task 5 array
//Include in portfolio
//Search Array

#include <iostream.h>
#include <cstring.h>


void main()

{


//Variable Declarations

string names[8];
string phoneno[8];
string sought;
int i;
int arraysize =8;
bool found;


//Obtain Data (Input)

names[0] = "Fred Smith";
names[1] = "Anne Jones";
names[2] = "Bill Bloggs";
names[3] = "Emily Brown";
names[4] = "Sid Black";
names[5] = "Mary Green";
names[6] = "Barney Blue";
names[7] = "Jack White";

phoneno[0] = "01633123456";
phoneno[1] = "01495987654";
phoneno[2] = "01504789345";
phoneno[3] = "02081453699";
phoneno[4] = "01496909087";
phoneno[5] = "01095432104";
phoneno[6] = "01356987654";
phoneno[7] = "03454123456";



//Processing (Search for the name & number)
//Get the name

cout<<"Enter required name: ";
getline (cin, sought);

//Find the name and displaying output
  found = false;
  for ( i = 0; i<arraysize;i++)
   {
     if (names[i] == sought)
        {
          cout<<"Found at position: "<<i<<endl;
          found = true;
          i = arraysize; //force loop to finish early if name is found
        }

   } // end of for i
   if (found == false)
      cout<<"Name "<< sought<< " not found"<<endl;

 } //end program

How would i link up the names to the phone numbers?? so if a certain name is inputted how would i display the phone numbers which are in the same index position but just on a different array??...
 
Last edited:
Back
Top Bottom