Structure & Filing

Description:

In this project you will be utilizing all your concepts that you have

learned in this semester related to programming specially related to

structures and filing.

In this assignment,

Firstly, create a structure named Contact which contain the following

attributes.

1. name

2. address

3. phone

Then, you have to create five functions as follow,

1. Add()

2. Search()

3. Delete()

4. Update()

5. Display()

In Add(), It has to take parameter of a type Contact structure and will

store it in a file.

In Search(), it has to take name as a parameter and will show the

record if exist in a file.

In Delete() It has to take name as a parameter and will delete the record

if exist in a file.

Update(), it will take name as a parameter. Then it will ask the user to

enter all the attributes i.e name, phone etc if the record is exist in a file. 

Display (), In this method, it will show all the records in the file. Each

line should display each contact record and all the attributes like name,

phone etc should be separated by a space.

In the main method, create a menu that has 6 options, Add,

delete, search, update, display all and exit.

The program should run infinitely and do not terminate until user enter

an option of �exit�.

NOTE: All these operations are developed using C++ filing.

Furthermore, the records will be stored in a single file.


//===========================================================Start=========================================================

#include<iostream>
#include <cstring>
#include<fstream>
using namespace std;
void Add();
void search();
void Delete();
void update();
void display();
struct student
{
    char name[50];
    char address[50];
    string phone;
   
};

int main()
{
char c,a,s;
cout<<"press y to add"<<endl;
cin>>a;
if(a=='y')
{
       
    Add();}
    else {
        cout<<"wrong input"<<endl;
    }
   
    cout<<"press y to search"<<endl;
    cin>>s;
    if(s=='y')
    {
    search();}
    else {
        cout<<"wrong input"<<endl;
    }
    cout<<"Press y for deleting, press z for updating and press d for display\n";
    cin>>c;
    if(c=='y'){
       
   
    Delete();
}
else if(c=='z'){

    update();
}
else if(c=='d'){
    display();
}
else{
    cout<<"wrong input"<<endl;
}

    return 0;
}


//====================================Adding Values=============================
void Add()
{
    student s;
    cout<<"Enter name :\n";
    cin>>s.name;
    cout<<"Enter Address :\n";
    cin>>s.address;
    cout<<"Enter Phone Number :\n";
    cin>>s.phone;
   
    ofstream project;
    project.open("project.txt");
    project << s.name<<"\n"<<s.address<<"\n"<<s.phone;
    project.close();
   
}

//====================================Searching Values=============================
void search()
{
    string mytext;
   
    ifstream project("project.txt");
    while(getline (project, mytext )){
        cout<<mytext<<"\n";
    }
    project.close();
   
//====================================Deleting Values=============================  
}
void Delete()
{
    string name,readd;
    int k,l=0;
    cout<<"Enter contact name you want to delete : ";
    cin>>name;
    ifstream myfile("project.txt");
    ofstream
    myfile1("fordlt.txt",ios::app);
    if(myfile.is_open()){
        string line;
        while(getline(myfile,line))
        {
            if((k=line.find(name))!=string::npos){
                cout<<endl;
                l++;
            }
            else{
                myfile1<<line<<endl;
            }
        }
       
        ofstream myfile3("project.txt");
       
        myfile3;
        myfile3.close();
        myfile.close();
        myfile1.close();
        ifstream read("fordlt.txt");
        ofstream
        write("project.txt",ios::app);
        while(getline(read,readd))
        {
            write<<read<<endl;
        }
        read.close();
        write.close();
        ofstream rewrite("fordlt.txt");
        rewrite;
        rewrite.close();
        if(l==0){
            cout<<"Contact Not Found :"<<endl;
        }
        else{
            cout<<"Contact Deleted Successfully: "<<endl;
        }
        myfile.close();
    }
    system("PAUSE");
    system("cls");
    main();
   
//====================================Updating Values=============================  
}
void update()
{
   
    string name, upname;
    int k, l=0;
    cout<<"update"<<endl;
    cin>>name;
    ifstream myfile("project.txt",ios::app);
    ofstream update("update.txt",ios::app);
    if(myfile.is_open()){
        string line;
        while(getline(myfile, line))
        {
           
            if((k=line.find(name))!=string::npos)
            {
                cout<<"Name : ";
                update<<"Name : ";
                cin.sync();
                getline(cin,upname);
                update<<upname<<"\t";
                cout<<"phone : ";
                update<<"phone : ";
                cin.sync();
                getline(cin,upname);
                update<<upname<<"\t";
                cout<<"City : ";
                update<<"City :";
                cin.sync();
                getline(cin,upname);
                update<<upname<<endl;
               
                l++;
            }
            else{
                update<<line<<endl;
               
            }
           
        }
        myfile.close();
        update.close();
        ofstream list("project.txt");
        list;
        list.close();
        ifstream upcon("update.txt",ios::app);
        ofstream con("project.txt",ios::app);
        while(getline(upcon,name)){
            con<<name<<endl;
        }
        upcon.close();
        con.close();
        ofstream rewrite("update.txt",ios::app);
        rewrite;
        rewrite.close();
        if(l==0)
        {
            cout<<"Contact not found :"<<endl;
        }
        else
        {
            cout<<"Contact updated successfully: "<<endl;
        }
        myfile.close();
    }
   
    system("PAUSE");
    system("cls");
    main();
   
}

//====================================Display Values=============================
void display()
{
    string mytext;
   
    ifstream project("project.txt");
    while(getline (project, mytext ))
    {
        cout<<mytext<<"\n";
    }
    project.close();
   
   
}
//===========================================================End=========================================================





cpp programming exercise with solution
cpp programming exercise with solution

cpp programming exercise with solution
cpp programming exercise with solution