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



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”
Tweets that mention 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 | ICT Trends - Online Computer Jobs Exam Preparation -- Topsy.com on August 14, 2010
[...] This post was mentioned on Twitter by Kannan B, ICT Trends. ICT Trends said: My recent post – Write a program using C++ to create a payroll system of any organization… http://goo.gl/fb/5L4s2 [...]
Jasmine on August 20, 2010
This piece of code looks pretty neat. It certainly brought back a lot of cool memories as I have done quite a bit of c++ programming myself years ago.
Chiropractic Longmont CO on August 21, 2010
I've also tried a menu driven program in c++ without using functions . But you have done using functions. Your's is seems to be easy.
froilan on March 15, 2011
i have a project in turbo c++, but i have no compiler of that . can you give me a site on where i can get a turbo c++ compiler???
tnx!>
Suresh Khanal
Twitter: icttrends
on March 17, 2011
I am working with Dev C++ a free c++ compiler and Microsoft Visual Studio 2005. Just google Dev c++ hope you’ll easily find the download link.
Casino Extras on June 8, 2011
Really interesting read – very kind of you to share all this information (I’d have kept it to myself, as I’m not the sharing type). May also give this Dev c++ a go.