Write a program using C++ to create a payroll system of any organization. Use functions for updating, displaying and deleting the record of given payroll system


This question was asked in Computer Officer Examination 2010 conducted by Public Service Commission. The question carried 10 mark. Following is the program for this question. I’ve tried to make it simpler and included most essential components only.

The program was created using C++ on Visual Studio 2005 with Service Pack 1 on Windows 7 operating system. It is thoroughly tested and without any warning and errors.

Download:

Download C++ Source Code of Payroll System – payroll.cpp


#include<iostream>
#include<fstream>
#include<string>

using namespace std;

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";
}

Tagged As: , , , ,

6 Responses to “Write a program using C++ to create a payroll system of any organization. Use functions for updating, displaying and deleting the record of given payroll system”

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Review this blog on Bloggers.com