Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<iostream.h>
class employee{
public:
int e_no;
float basic,deduction,allowence;
char name[50],address[50];
void add();//add new records.
void dis();//display records.
};
employee payroll;
//********main()*********
main(){
clrscr();
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<<"\n0-> Exit"<<endl;
cout<<endl<<endl;
cout<<"selection: ";cin>>choice;
if(choice<0 || choice>4){
clrscr();
main();
}
switch(choice){
case 1:payroll.add(); break;
case 2:payroll.dis(); break;
case 0:exit(0);
}
getch();
return 0;
}
void employee::add(){
clrscr();
FILE*fptr;
int choice;
employee e;
do{
cout<<"========Enter New Record========"<<endl<<endl;
cout<<"Enter Employee Number: ";cin>>e_no;
cout<<"Enter Employee Name : ";gets(e.name);
cout<<"Enter Address : ";gets(e.address);
cout<<"Enter Basic Salary : ";cin>>e.basic;
cout<<"Enter Allowence : ";cin>>e.allowence;
cout<<"Enter Deduction : ";cin>>e.deduction;
cout<<endl<<endl;
cout<<"1->Save | 2->Cancel"<<endl;cin>>choice;
}while(!(choice==1 || choice==2));
if(choice==1){
fptr=fopen("payroll.dat","w");
fprintf(fptr,"%d\n",e_no);
fprintf(fptr,"%s\n",e.name);
fprintf(fptr,"%s\n",e.address);
fprintf(fptr,"%f\n",e.basic);
fprintf(fptr,"%f\n",e.allowence);
fprintf(fptr,"%f\n",e.deduction);
fclose(fptr);
cout<<"Record Saved"<<endl<<endl;
}else
cout<<"Record Cancelled";
cout<<"Enter Another Record? "<<endl;
cout<<"1->Yes | 2->No"<<endl;cin>>choice;
if(choice==1)
add();
else
main();
}
void employee::dis(){
int num;
FILE*fptr;
employee e;
cout<<"Enter Employee Number: ";cin>>num;
if(num==e_no){
fptr=fopen("payroll.dat","r+");
fscanf(fptr,"%d\n",&e_no);
fscanf(fptr,"%s\n",&e.name);
fscanf(fptr,"%s\n",&e.address);
fscanf(fptr,"%f\n",&e.basic);
fscanf(fptr,"%f\n",&e.allowence);
fscanf(fptr,"%f\n",&e.deduction);
cout<<"\nNumber :"<<e_no;
cout<<"\nName : "<<e.name;
cout<<"\nAddress : "<<e.address;
cout<<"\nBasic Salary : "<<e.basic;
cout<<"\nAllowence : "<<e.allowence;
cout<<"\nDeductions : "<<e.deduction<<endl;
cout<<endl;
cout<<"\nTotal Salary :\t"<<e.basic+e.allowence;
cout<<"\nNet Paid :\t"<<e.basic+e.allowence-e.deduction;
fclose(fptr);
}
else
cout<<"wrong";
}
Here is my code. My problem is that when I enter information of an employee and then without exiting output screen I see saved record of that employee then program works properly.
But when I quit to program and again compile and run program and enters a employee number for which I want to see a record then it doesn't show any program but shows main menu. Is my file doesn't save properly?
What is problem with my code?
And secondly if I wish to add multiple employee record and want to see individual information by given their employee number then program must show only that employee's information for which I have entered employee number.
I hope you understand my question. Help me please.