dimanche 18 novembre 2018

PhoneBook Application Using C++ File Handling problem

I made a phone book application using C++ and adding file handling as an Assignment.

I am having certain problems as I have yet not studied file handling in detail and is noob when it comes to programming. I am having problem here with file handling. The code is incomplete for group creation, however it's not even working properly for writing and reading into file. I don't understand what mistakes did I do.

I understand that the code is made complex. I would Like you to help me how can I make it work.

I am sorry if I am asking something like which goes against the rules of this community.

The application is a console menu driven program to

  • show all contacts Inserted
  • Enter New Contact
  • Edit contact
  • Search contact
  • Remove Contact
  • some other stuffs
  • exit

The problem arises in writing and reading the file in functions

  • write(); doesn't write .txt in readable format
  • readall(); doesn't display the data correctly

Full Code-

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//system("cls");
using namespace std;

int checkName(string const &x);
int checkPhno(string const &x);
int checkEmail(string const &x);

class phoneBook/*::private FileHandler*/{
    string name;
    string phno;
    string email;
    public:
        void edit();
        void putdata();
        void showdata();
        void update();
        void changeRecord(phoneBook const &x);
        void write();
        void readall();
        void data();
        void del(string const n);
        void search();

        string getname(){
            return name;
        }
        string getphno(){
            return phno;
        }
        string getemail(){
            return email;
        }       

};

void phoneBook :: putdata(){
    cout<<"\nEnter Name          : ";
    cin.ignore();
    getline(cin,name);
    cout<<"\nEnter Phone Number  : ";
    cin.ignore();
    getline(cin,phno);
    cout<<"\nEnter Email         : ";
    cin.ignore();
    getline(cin,email);
    while(!checkName(name)){
        cout<<"Name Already Exisits!!!!!\n";
        cout<<"Enter again. Or press Esc and then Enter to Cancel. \n";
        cin.ignore();
        getline(cin,name);
        if(name[0]==27)
            return;
    }
    while(!checkPhno(phno)){
        cout<<"Phone No. Already Exisits!!!!!\n";
        cout<<"Enter again. Or press Esc and then Enter to Cancel. \n";
        cin.ignore();
        getline(cin,phno);
        if(phno[0]==27)
            return;
    }
    while(!checkEmail(email)){
        cout<<"Email Already Exisits!!!!!\n";
        cout<<"Enter again. Or press Esc and then Enter to Cancel. \n";
        getline(cin,email);
        cin.ignore();
        if(name[0]==27)
            return;
    }
    write();
}

void phoneBook :: data(){
    //system("cls");
    cout<<"Name:               "<<name<<endl;
    cout<<"Phone No.           +91 "<<phno<<endl;
    cout<<"Email:              "<<email<<endl;
}
void phoneBook :: showdata(){
    data();
    cout<<"1. Edit\n2. Delete\n3. Return\n";
    int c;
    cin>>c;
    switch(c){
        case 1:
            update();
            break;
        case 2:
            del(getname());
            break;
        case 3:
            return;
        default:
            cout<<"Wrong Choice!!! Returning Back!!!!!\n";
    }
}

void phoneBook :: update(){
            int ch=0;
            phoneBook temp;
            cout<<"What would You like to update \n";
            cout<<"1. Name\n";
            cout<<"2. Phone no.\n";
            cout<<"3. Email Address\n";
            cout<<"4. Done\n";
            do{
                cin>>ch;
                switch(ch){
                    case 1:
                        cin.ignore();
                        getline(cin,temp.name);
                        //name=temp;
                        break;
                    case 2:
                        cin.ignore();
                        getline(cin,temp.phno);
                        //phno=temp;
                        break;
                    case 3:
                        cin.ignore();
                        getline(cin,temp.email);
                        //email=temp;
                        break;
                    case 4:

                        break;
                    default:
                        cout<<"Wrong choice! Enter again: ";
                        break;
                }
                cout<<"Enter the choice: ";
            }while(ch!=4);
            changeRecord(temp);
}

void  phoneBook :: changeRecord(phoneBook const &x){
    phoneBook tmp;
    fstream file("PhoneBook.txt", ios::in|ios::out|ios::ate);
    file.seekg(0);
    do{
        file.read((char *)&tmp,sizeof(tmp));
        if((this->name).compare(tmp.name)){
        //  file.seekp(file.tellp()-sizeof(tmp));
            file.seekp(-sizeof(tmp),ios::cur);
            file.write((char*)&x,sizeof(x));
        }   
    }while(!file.eof());

}
void phoneBook :: search(){

}
void  phoneBook :: write(){
        ofstream file_w;
        file_w.open("PhoneBook.txt", ios::app); 
        file_w.write((char*)this, sizeof(*this)); //this signifies that the object which called this function

        file_w.close();
        cout<<"Contact Inserted\n"; 
    } 

void phoneBook :: readall(){
    phoneBook x;
    ifstream file_r;
    file_r.open("PhoneBook.txt", ios::in);
    if(!file_r){
        cout<<"File Not Found\n";
        return;
    }

    do{
        file_r.read((char*)&x, sizeof(x));
        x.data();
    }while(file_r.eof());
    file_r.close();
}

void phoneBook :: del(string const n){
        ifstream file_r;
        file_r.open("PhoneBook.txt", ios::in);
        ofstream file_w;
        file_w.open("temp.txt",ios::out);
        if(!file_r){
            cout<<"File Not Found\n";
            return;
        }
        while(!file_r.eof()){
            file_r.read((char*)this, sizeof(*this));
            if( !n.compare(name) ){
                file_w.write((char*)this, sizeof(*this));
            }
        }

        file_r.close();
        file_w.close();
        remove("PhoneBook.txt");
        rename("temp.txt","PhoneBook.txt");

}
void phoneBook :: edit(){
    search();
    update();
    cout<<"Updated Contacts: \n";
    showdata();
    //delay(500);
}

int checkName(string const &x){
    return 1;
}
int checkPhno(string const &x){
    return 1;
}
int checkEmail(string const &x){
    return 1;
}

int main(){
    phoneBook p;
    enum {
        Show = 1,
        Add,
        Edit,
        Search,
        Remove,
        Create,
        View,
        Exit
    };
    string choice;
    int ch;
    bool Continue=true;

    while(Continue){
        //system("cls");
        cout << "\nWhat would you like to do?: " << "\n";
        cout << "1. Show All Contacts" << "\n";
        cout << "2. Add A Contact" << "\n";
        cout << "3. Edit A Contact" << "\n";
        cout << "4. Search Contacts" << "\n";//
        cout << "5. Remove A Contact" << "\n";
        cout << "6. Create Groups" << "\n";//
        cout << "7. View Groups" << "\n";//
        cout << "8. Exit The Program" << "\n\n";
        //getline(cin,choice);
        cin>>choice;
        try{
            ch=stoi(choice);
        }
        catch(...){
            ch=0;
        }
        switch(ch){
            case Show:
                            p.readall();
                            break;
            case Add:
                            p.putdata();
                            break;
            case Edit:
                            p.update();
                            break;
            case Search:
                            p.search();
                            break;
            case Remove:
                            p.search();
                            p.del("abc");
                            break;
            case Create:

                            break;
            case View:

                            break;
            case Exit:
                            Continue=false;
                            break;
        }

    }
    cout<<"Exiting!!!!!\n";
    return 0;
}

some other issues are there which I think I might solve if I can solve the above problems.

Aucun commentaire:

Enregistrer un commentaire