Comment each line and convert this C++ OOP code into just C++

Discussion in 'C++' started by Shayaan_Mustafa, Jan 3, 2011.

  1. Shayaan_Mustafa

    Shayaan_Mustafa New Member

    Joined:
    Dec 25, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student.
    Location:
    Karachi
    Code:
    #include<iostream>
    #include<fstream>
    #include<string>
    struct emp{
        int eno;
        char name[50],address[50];
        float basic,allowence,deduction;
    };
    void addEmp(); //adds new record to data file
    void disEmp(); //displays all records from file
    void delEmp(); //asks empNo and removes that record from file
    void updEmp(); //asks empno and lets you reenter that data
    void cls(); //just clear screen
    int main(){
        string dummy;
        int choice;
        cout<<"===== Main Menu ====="<<endl;
        cout<<endl<<endl;
        cout<<"1 -> Add Record "<<endl;
        cout<<"2 -> Display Record"<<endl;
        cout<<"3 -> Delete Record"<<endl;
        cout<<"4 -> Update Record"<<endl;
        cout<<endl;
        cout<<"0 -> Exit"<<endl;
        cout<<endl<<endl<<"Selection ";
        cin>>choice;
        if(choice<0 || choice>4){
            cls();
            main();
        }
        switch(choice){
            case 0:    break;
            case 1:    addEmp();    break;
            case 2:    disEmp();    break;
            case 3:    delEmp();    break;
            case 4:    updEmp();    break;
        }
    }
    void addEmp(){
        fstream data;
        string dummy;
        emp e;
        int choice;
        do{
            cls();
            cout<<"===== Enter New Record ====="<<endl;
            cout<<endl<<endl;
            cout<<"Eno :\t\t"; cin>>e.eno;
            getline(cin,dummy); //just to clear input buffer
            cout<<"\nName :\t\t"; gets_s(e.name,50);
            cout<<"\nAddress :\t"; gets_s(e.address,50);
            cout<<"\nBasic Salary :\t";cin>>e.basic;
            cout<<"\nAllowence :\t";cin>>e.allowence;
            cout<<"\nDeductions :\t";cin>>e.deduction;
            cout<<endl<<endl;
            cout<<"1 -> Save  | 2 -> Cancel"<<endl;
            cin>>choice;
        }while(!(choice==1 || choice==2));
        if(choice==1){
            data.open("data.dat",ios::out|ios::app);
            data.write((char*)&e,sizeof(e));
            data.close();
            cout<<"\nRecord Saved "<<endl<<endl;
        }else{
            cout<<"\n\nRecord Cancelled"<<endl<<endl;
        }
        cout<<"Enter another record?"<<endl;
        cout<<"1 -> Yes  | 2 -> No"<<endl;
        cin>>choice;
        if(choice==1)
            addEmp();
        else
            return;
        main();
    }
    void disEmp(){
        fstream data;
        emp e;
        data.open("data.dat",ios::in);
        cls();
        while(data.read((char*) &e, sizeof(e))){
            cout<<"\n\n-----Displaying record for ENO " <<e.eno<<endl;
            cout<<"\nName :\t\t"<<e.name;
            cout<<"\nAddress :\t"<<e.address;
            cout<<"\nBasic Salary :\t"<<e.basic;
            cout<<"\nAllowence :\t"<<e.allowence;
            cout<<"\nDeductions :\t"<<e.deduction<<endl;
            cout<<endl<<"\nTotal Salary :\t"<<e.basic+e.allowence;
            cout<<"\nNet Paid :\t"<<e.basic+e.allowence-e.deduction;
            cout<<endl;
        }
        data.close();
        main();
    }
    void delEmp(){
        fstream data, temp;
        emp e;
        int eno;
        cls();
        cout<<"===== Delete Record ====="<<endl;
        cout<<endl<<endl<<endl;
        cout<<"Enter ENO to delete record ";cin>>eno;
        rename("data.dat","temp.dat");
        data.open("data.dat",ios::app);
        temp.open("temp.dat",ios::in);
        temp.seekg(0,ios::beg);
        while(temp.read((char*)&e,sizeof(e))){
            if(!(e.eno==eno)){
                cout<<"Record to delete "<<e.eno;
                data.write((char*) &e,sizeof(e));
            }
        }
        data.close();
        temp.close();
        system("Del temp.dat");
        main();
    }
    void updEmp(){
        fstream data, temp;
        emp e;
        string dummy;
        int eno;
        cls();
        cout<<"===== Update Record ====="<<endl;
        cout<<endl<<endl<<endl;
        cout<<"Enter ENO :_";cin>>eno;
        rename("data.dat","temp.dat");
        data.open("data.dat",ios::app);
        temp.open("temp.dat",ios::in);
        temp.seekg(0,ios::beg);
        while(temp.read((char*)&e,sizeof(e))){
            if(e.eno==eno){
                cout<<"-----Current data for ENO: "<<e.eno;
                cout<<"\nName :\t\t"<<e.name;
                cout<<"\nAddress :\t"<<e.address;
                cout<<"\nBasic Salary :\t"<<e.basic;
                cout<<"\nAllowence :\t"<<e.allowence;
                cout<<"\nDeductions :\t"<<e.deduction<<endl;
                cout<<endl<<"\nTotal Salary :\t"<<e.basic+e.allowence;
                cout<<"\nNet Paid :\t"<<e.basic+e.allowence-e.deduction;
                cout<<endl<<endl;
                cout<<"-----Enter New Values-----"<<endl;
                cout<<"\nEno :\t\t"; cin>>e.eno;
                getline(cin,dummy); //just to clear input buffer
                cout<<"\nName :\t\t"; gets_s(e.name,50);
                cout<<"\nAddress :\t"; gets_s(e.address,50);
                cout<<"\nBasic Salary :\t";cin>>e.basic;
                cout<<"\nAllowence :\t";cin>>e.allowence;
                cout<<"\nDeductions :\t";cin>>e.deduction;
            }
            data.write((char*) &e,sizeof(e));
        }
        data.close();
        temp.close();
        system("Del temp.dat");
        main();
    }
    void cls(){
        for (int i=0;i<50;i++)
            cout<<"\n";
    }

    At least must answer one. Please
    Comment each and every line in this program.
    Also how to convert this C++ object-oriented code in just C++ so that it could be complied on "Turbo C++ IDE" i.e. it can be able to use header files of .h extesion.
    Thanks in advance. Thanks a lot.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice