Develop a telephone directory which is capable of doing the following:

  1. Insert a contact
  2. Delete a contact
  3. Edit an existing contact (changing or modifying a record)
  4. Search a contact (and display complete record on screen)
    a. Search through first name
    b. Search through last name
    c. Search through landline
    d. Search through cell number
  5. Display all contacts that:

    a. Start with a given letter (e.g., all contacts starting with ‘M’)
    b. Landline or cell number starting with a particular pattern (e.g., 0300 for cell number, or
    (042)3518 for landline number)

  6. Sort all contacts with respect to
    a. first name
    b. last name
    c. land line number
    d. mobile number

  7. Exit

Directions:
All the contacts are to be stored in a file, contacts.dat (all records are sorted with respected to first
name). You have to update the existing file if a contact is inserted, deleted or edited. If searching is
applied, the record(s) should be displayed on the output screen only. In case of sorting, new file
should be created (the name of the new file should be entered by the program user). Note that all
fields of a single record are comma separated (no commas otherwise). You are not allowed to use
data type string in any case. Make separate functions to perform each task. Inputs are to be taken in
the main function only; cin cannot be used in any function other than main.
Your program should be menu driven. The program should exit if the user choice is 7.

A sample record will be as follows:

FirstName<space>LastName,<space>Address,<space>CellPhone,<space>LandLine<\n>

Note:
1. An address may include comma too, e.g. 123-H1, Johar Town, Lahore.
2. A cell phone number will only start with a 0.
3. Middle name cannot be entered in the record.
4. All entries will have first name, last name, address, cell phone and landline, none of the entry
can be skipped.
5. Maximum number of records can be 1000.
6. Maximum number of characters (FirstName) should not exceed 15 characters.
7. Maximum number of characters (LastName) should not exceed 15.
8. Maximum number of characters (Address) should not exceed 30.

Recommended Answers

All 26 Replies

First use ifstream object to open the file for reading.
In a loop use ifstream's >> operator to read the data into each variable

That's the best advice I can give you since you failed to tell us anything at all about what you have done so far to solve the problem.

Can you give me the code for this ?

Yes I could, but I won't. We don't do people's homework for them. Instead, you give it a try, post the code you have written, then ask questions about what you don't undestand.

#include<iostream>
#include<fstream>


using namespace std;

struct telephone
{
       char firstname[15];
       char lastname[15];
       char address[30];
       char cell[15];
       char landline[15];
};

void GetInput (ifstream&, telephone[], int&);
void PrintData (telephone[], int);

int main()
{   ifstream fin;
    ofstream fout;
    fin.open("data.txt");



int n=0;

telephone readData[1000];

GetInput (fin, readData, n);





//for inserting a new contact (will make a function of this after it is done)

cout<<"Enter First name"<<endl;
cin>>readData[n].firstname;

cout<<"Enter Last name"<<endl;
cin>>readData[n].lastname;

cout<<"Enter Address"<<endl;
cin.getline(readData[n].address,n);

cout<<"Enter Cell number"<<endl;
cin>>readData[n].cell;

cout<<"Enter Landline number"<<endl;
cin>>readData[n].landline;

PrintData(readData,n);


system ("pause");
return 0;


}

void GetInput (ifstream& filein, telephone data[], int& count)
{
       while (!filein.eof())
    {
        filein.getline (data[count].firstname, 15, ' ');
        filein.getline (data[count].lastname, 15, ',');
        filein.getline (data[count].address, 30, ',');
        filein.getline (data[count].cell, 15, ',');
        filein.getline (data[count].landline, 15, '\n');
        count++;
    }

}

void PrintData(telephone data[], int size)
{
    cout << endl;
    for (int i=0; i<size+1;i++)
    {
        cout << "First Name: " << data[i].firstname << endl;
        cout << "Last Name: " << data[i].lastname << endl;
        cout << "Address: " << data[i].address << endl;
        cout << "Cell: " << data[i].cell << endl;
        cout << "Landline: " << data[i].landline << endl;
        cout << endl;
    }
}

Right now my major problem is that in the part where im taking input from user to insert a new contact, what is the right way to take the address? It isnt saving spaces. And the second problem is that how can i update the file with this contact that im inserting?

It is skipping the line that calls getline() because you are mixing formatted and unformatted data input. When someone enters something he/she always presses the <Enter> key. cin>> does not extract it from the keyboard buffer, so when getline() is called it thinks its the end of data input.

To solve the problem you have to remove all keystrokes from the keyboard input buffer before calling getline() after using >> operator. N ote the use of your getline() was also incorrect. See correction below.

#include <limits>

...
...

cout<<"Enter Last name"<<endl;
cin>>readData[n].lastname;
cin.ignore( numeric_limits <streamsize> ::max(), '\n' ); // empty the keyboard buffer
cout<<"Enter Address"<<endl;
cin.getline(readData[n].address, sizeof(readData[n].address));

cout<<"Enter Cell number"<<endl;
cin>>readData[n].cell;

Thank you so much! Can you also tell me that how can I update the file by inserting the contact details taken from the user? (btw only character arrays are allowed)

You can do one of two ways:
1. Completely rewrite the file adding the new record in any order.
2. Open the file for appending and write it to the end of the file. This is probably the better of the two options.

.........
.........

cout<<"Enter First name"<<endl;
cin>>readData[n].firstname;

cout<<"Enter Last name"<<endl;
cin>>readData[n].lastname;

cin.ignore( numeric_limits <streamsize> ::max(), '\n' ); // empty the keyboard buffer
cout<<"Enter Address"<<endl;
cin.getline(readData[n].address, 30);

cout<<"Enter Cell number"<<endl;
cin>>readData[n].cell;

cout<<"Enter Landline number"<<endl;
cin>>readData[n].landline;


fout<<readData[n].firstname;
fout<<readData[n].lastname;
fout<<readData[n].address;
fout<<readData[n].cell;
fout<<readData[n].landline;

it isnt doing anything ( the file is opened and ofstream declared)

post the line that opens the output file. Also, you need to put a separator after each line 21-25 so that those lines can be read back into the program. For example:

fout << readData[n].firstname << ' ' << readData[n].lastname << ' ' << readData[n].address << ',' << readData[n].cell << ' ' << readData[n].landline << '\n';

ifstream fin;
ofstream fout;
fin.open("data.txt");

notice that you didn't open the output file.

am i doing it wrong?

I want to store the data in the same file in which the data is already stored

Yes, but the output file isn't opened. All you opened was the input file. You need to call fout.open() similar to what you did with fin.open(), except you have to add another parameter so that open() won't destroy the file's contents. See the description here for the values of the second parameter.

example: fout.open("data.txt", ios::ate); <<< Open the file for Appending

It is still overwriting the file's contents, now what should i do?

should be ios::ape, not ios::ate

sorry, i found the problem. The file was already opened by fin.open that was creating problem.

still not fixed :/

The file was already opened by fin.open that was creating problem.

No, that was not the problem.

Open fout like this: fout.open("data.txt", ios::app);

I did this but the console screen gets stuck.

post the complete program

#include<iostream>
#include<fstream>
#include<limits>

using namespace std;

struct telephone
{
       char firstname[15];
       char lastname[15];
       char address[30];
       char cell[15];
       char landline[15];
};

void GetInput (ifstream&, telephone[], int&);
void PrintData (telephone[], int);
void insert (ofstream&, telephone[], int&);

int main()
{   ifstream fin;
    ofstream fout;

    fin.open("data.txt");
    fout.open("data.txt", ios::app);

    int n=0;
    int option;
    telephone readData[1000];

    cout<<" ****************************Telephone Directory****************************"<<endl;
    cout<<" *                                                                         *"<<endl;
    cout<<" * Enter '1' to Insert a contact.                                          *"<<endl;
    cout<<" * Enter '2' to Delete a contact.                                          *"<<endl;
    cout<<" * Enter '3' to Edit an existing contact (changing or modifying a record). *"<<endl;
    cout<<" * Enter '4' to Search a contact (and display complete record on screen):  *"<<endl;
    cout<<" *        Enter 'a' to Search through first name.                          *"<<endl;
    cout<<" *        Enter 'b' to Search through last name.                           *"<<endl;
    cout<<" *        Enter 'c' to Search through landline.                            *"<<endl;
    cout<<" *        Enter 'd' Search through cell number.                            *"<<endl;
    cout<<" * Enter '5' Display all contacts that:                                    *"<<endl;
    cout<<" *         Enter 'a' for contacts that Start with a given letter           *"<<endl;
    cout<<" *         (e.g., all contacts starting with M).                           *"<<endl;
    cout<<" *                                                                         *"<<endl;
    cout<<" *         Enter 'b' to display Landline or cell number starting with      *"<<endl; 
    cout<<" *         a particular pattern (e.g., 0300 for cell number, or            *"<<endl;
    cout<<" *         (042)3518 for landline number).                                 *"<<endl;

    cout<<" * Enter '6' to Sort all contacts with respect to:                         *"<<endl;
    cout<<" *         First name, Enter 'a'.                                          *"<<endl;
    cout<<" *         Last name, Enter 'b'.                                           *"<<endl;
    cout<<" *         Land line number, Enter 'c'.                                    *"<<endl;
    cout<<" *         Mobile number, Enter 'd'.                                       *"<<endl;
    cout<<" * Press '7' to Exit.                                                      *"<<endl;
    cout<<" ***************************************************************************"<<endl;
    cin>>option;


    switch (option)
    {
           case 1: GetInput (fin, readData, n);

                   cout<<"Enter First name"<<endl;
                   cin>>readData[n].firstname;

                   cout<<"Enter Last name"<<endl;
                   cin>>readData[n].lastname;

                   cin.ignore( numeric_limits <streamsize> ::max(), '\n' ); // empty the keyboard buffer
                   cout<<"Enter Address"<<endl;
                   cin.getline(readData[n].address, 30);

                   cout<<"Enter Cell number"<<endl;
                   cin>>readData[n].cell;

                   cout<<"Enter Landline number"<<endl;
                   cin>>readData[n].landline;

                   insert (fout, readData, n);

                   break;

}

    PrintData(readData,n);

    system ("pause");
    return 0;
}

void insert (ofstream& fileout, telephone readData[], int& n)
{

    fileout << readData[n].firstname << ' ' << readData[n].lastname << ',' << readData[n].address;
    fileout<< ',' << readData[n].cell << ','<< readData[n].landline << '\n';

}


void GetInput (ifstream& filein, telephone data[], int& count)
{

       while (!filein.eof())
    {   
        filein.getline (data[count].firstname, 15, ' ');
        filein.getline (data[count].lastname, 15, ',');
        filein.getline (data[count].address, 30, ',');
        filein.getline (data[count].cell, 15, ',');
        filein.getline (data[count].landline, 15, '\n');
        count++;
    }

}

void PrintData(telephone data[], int size)
{
    cout << endl;
    for (int i=0; i<size;i++)
    {
        cout << "First Name: " << data[i].firstname << endl;
        cout << "Last Name: " << data[i].lastname << endl;
        cout << "Address: " << data[i].address << endl;
        cout << "Cell: " << data[i].cell << endl;
        cout << "Landline: " << data[i].landline << endl;
        cout << endl;
    }
}

A couple things incorrect.

  1. Open the files only in the functions in which they are used, then close them before leaving that function. Don't open the files in main() because main() doesn't need them. When you do that you will have to delete the first parameter to both insert() and getinput().

  2. In getinput(), make a check to insure the file was opened for writing successfully. If the file doesn't exist the loop in the function will become an infinite loop. So call is_open() before starting to read the file.

Also, don't use eof() because it will cause the last line of the file to be read twice, instead use this:

void GetInput (telephone data[], int& count)
{
    ifstream fin;
    fin.open("data.txt");

    if( fin.is_open())
    {
        while (fin.getline (data[count].firstname, 15, ' '))
        {   
        fin.getline (data[count].lastname, 15, ',');
        fin.getline (data[count].address, 30, ',');
        fin.getline (data[count].cell, 15, ',');
        fin.getline (data[count].landline, 15, '\n');
        count++;
        }
    }
}

fast students are not allowed to take help from internet in order to correct the program.This program if submitted by any student would be marked 0 and the student will have to appear in front of disciplinary comittee.
FAST-NUCES

Oops!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.