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

This is a discussion on Comment each line and convert this C++ OOP code into C++ code. within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    4

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

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,850
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by Shayaan_Mustafa
    At least must answer one. Please
    Comment each and every line in this program.
    Why would you want to comment each and every line? If you are hoping to have the program explained line by line to you, then that might make sense, but it also means that you are probably biting off more than you can chew since you should only need the newer parts to be explained.

    Quote Originally Posted by Shayaan_Mustafa
    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.
    That does not make sense. The code in question is not particularly object oriented anyway. Perhaps you are asking for how to modify the code so that it could compile on your compiler (to which my answer may well be: upgrade to a newer compiler instead), or perhaps you are asking how to appropriately use header files here.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Well I'll add my 2c and comment one line:
    Code:
        main(); // The C++ standard forbids calling main recursively,
                // but this program naughtily does just that. Undefined behaviour - sheesh!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert ASM to C Code
    By clag in forum C Programming
    Replies: 2
    Last Post: 01-28-2010, 03:48 AM
  2. Need the missing line of code
    By mayhem in forum C Programming
    Replies: 3
    Last Post: 06-20-2005, 05:21 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21