dimanche 27 mai 2018

C++ Program On Elementary Database Operations i.e. Record Management

When i execute the program and select option number 2 it reads the data from the file successfully but when i select the option number 2 for the second time , third time and so on... nothing comes up and it returns back to the menu. I have tried so many times, same error comes every time. PLEASE HELP. Thanks to all in advance. love all.

#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <iomanip>
#include <iostream>

using namespace std;

//class for database functions
class group
{
private:
    struct person {
            char flag;
            char empcode[5];
            char name[10];
            int age;
            float sal;
    }p;
public:
    fstream file,t_file;
    ifstream fin;
    ofstream fout;
    group();
    void addrec();
    void listrec();
    void modirec();
    void delrec();
    void recovrec();
};

//main function
int main()
{
    char choice;
    group g;
    do
    {
        system("cls");
        cout<<"1.Add Records"<<endl;
        cout<<"2.List Records"<<endl;
        cout<<"3.Modify Records"<<endl;
        cout<<"4.Delete A Record"<<endl;
        cout<<"5.Recover Deleted Records"<<endl;
        cout<<"6.Exit"<<endl;
        cout<<"Your choice ?: ";
        cin>>choice;
        system("cls");
        switch(choice)
        {
            case '1':
                g.addrec();
                break;
            case '2':
                g.listrec();
                break;
            case '3':
                g.modirec();
                break;
            case '4':
                g.delrec();
                break;
            case '5':
                g.recovrec();
                break;
            case '6':
                exit(0);
        }
    } while(choice!=6);

    return 0;
}

//default constructor
group::group()
{
    file.open("EMP.DAT",ios::in|ios::out|ios::binary);
    p.flag=' ';
    if(!file)
        cerr<<"Unable to open file";
}

//Add records at the end
void group::addrec()
{
    char ch;
    cout<<"Do You Want To Erase Previous Records ?: ";
    cin>>ch;
    if(ch=='y'||ch=='Y')
        remove("EMP.DAT");
    fout.open("EMP.DAT",ios::binary);
    fout.seekp(0L,ios::end);
    system("cls");
    do
    {
        cout<<"Enter Emp_Code, Name, age, Salary"<<endl;
        cin>>p.empcode>>p.name>>p.age>>p.sal;
        fout.write((char*)&p,sizeof(p));
        cout<<"Add another record ?: ";
        cin>>ch;
    } while(ch=='y'||ch=='Y');

    fout.close();
}

//list all the records
void group::listrec()
{
    int j=0;
    //position get pointer at beginning
    //fin.open("EMP.DAT",ios::binary);
    file.seekg(0L,ios::beg);
    while(file.read((char*)&p,sizeof(p)))
    {
        if(p.flag!='*')
        {
            cout<<"\nRecord N. "<<++j;
            cout<<"\nEmp Code: "<<p.empcode;
            cout<<"\nName: "<<p.name;
            cout<<"\nAge: "<<p.age;
            cout<<"\nSalary: "<<p.sal<<endl;
        }
    }
    cin.ignore();
    getchar();
}

//modifies a given record from file
void group::modirec()
{
    char code[5];
    int count=0;
    long int pos;
    cout<<"Enter Emp Code";
    cin>>code;
    file.open("EMP.DAT",ios::in|ios::out|ios::binary);
    file.seekg(0,ios::beg);
    //search an employee
    while(file.read((char*)&p,sizeof(p)))
    {
        //if record is found
        if(strcmp(p.empcode,code)==0&&p.flag==' ')
        {
            //recieve new data
            cout<<"Enter new Data: "<<endl;
            cout<<"Enter Emp_Code, Name, age, Salary"<<endl;
            cin>>p.empcode>>p.name>>p.age>>p.sal;
            //position put pointer to overwrite record
            pos=count*sizeof(p);
            file.seekp(pos,ios::beg);
            file.write((char*)&p,sizeof(p));
            file.close();
            cout<<endl<<"Press Any Key...";
            getchar();
            return;
        }
        count++;
    }
    file.close();
    cout<<endl<<"No Employee In File With Code: "<<code;
    cout<<endl<<"Press Any Key...";
    getchar();
}

//marks a record for deletion
void group::delrec()
{
    char code[5];
    long int pos;
    int count =0;
    cout<<"Enter Emp Code";
    cin>>code;
    file.open("EMP.DAT",ios::in|ios::out|ios::binary);
    //position pointer at beginning
    file.seekg(0L,ios::beg);
    //search an employee
    while(file.read((char*)&p,sizeof(p)))
    {
        //if record is found
        if(strcmp(p.empcode,code)==0&&p.flag==' ')
        {
            p.flag='*';
            //position put pointer
            pos=count*sizeof(p);
            file.seekp(pos,ios::beg);
            file.write((char*)&p,sizeof(p));
            file.close();
            return ;
        }
        count++;
    }
    cout<<endl<<"No Employee In File With Code: "<<code;
    file.close();
    cout<<endl<<"Press Any Key...";
    getchar();
}

//unmarkmarks a record
void group::recovrec()
{
    char code[5];
    long int pos;
    int count =0;
    cout<<"Enter Emp Code";
    cin>>code;
    file.open("EMP.DAT",ios::in|ios::out|ios::binary);
    //position pointer at beginning
    file.seekg(0L,ios::beg);
    //search an employee
    while(file.read((char*)&p,sizeof(p)))
    {
        //if record is found
        if(strcmp(p.empcode,code)==0&&p.flag=='*')
        {
            p.flag=' ';
            //position put pointer
            pos=count*sizeof(p);
            file.seekp(pos,ios::beg);
            file.write((char*)&p,sizeof(p));
            file.close();
            return ;
        }
        count++;
    }
    file.close();
    cout<<endl<<"No Employee In File With Code: "<<code;
    cout<<endl<<"Press Any Key...";
    getchar();
}

Aucun commentaire:

Enregistrer un commentaire