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:
Personally i'd define a class called Contact, that would have member data such as FirstName, LastName, PhoneNumber etc, and store these in a vector<Contact>.....
 
Create a class and instansiate an object of that class... but for now... just create a record using a struct.

Code:
    struct details {
        string name;
        string phoneno;
    }

    details people[8];

    details[0].name = 'woz';
    details[0].phoneno = '123123';
    details[1].name = 'jobs';
    details[1].phoneno = '456456';
 
String is not a valid identifier, use char names[] instead. None of those constants will work either. Instead of getline use cin.getline(name, 8, '\n') where \n (return) can be replaced with whatever you want to end the input.
 
PanMaster said:
String is not a valid identifier, use char names[] instead. None of those constants will work either. Instead of getline use cin.getline(name, 8, '\n') where \n (return) can be replaced with whatever you want to end the input.


String is an object type that is used in some APIs (STL). For example, AnsiString in Borland is a class... and you can use AnsiString name; this will create an AnsiString object.
 
Last edited:
the :: syntax is used when you create the implementation of a class function definition... it is used to refer to the definition, not to create an object instance of the class.

edit:

sorry my mistake. you are correct. Is it actually part of the ANSI standard?
 
Last edited:
nomore said:
the :: syntax is used when you create the implementation of a class function definition... it is used to refer to the definition, not to create an object instance of the class.

edit:

sorry my mistake. you are correct. Is it actually part of the ANSI standard?

Yes - its part of the STL, which is in turn, part of standard c++.
 
Back
Top Bottom