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");
fwrite(&e,sizeof(employee),1,fptr);
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(){
FILE*fptr;
employee e;
fptr=fopen("payroll.dat","r+");
while(fread(&e,sizeof(employee),1,fptr)){
cout<<"Enter Employee Number To Display Record: ";cin>>e.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);
main();
}
My problem is that when I enter all information and press 1 to save record, it is saved. But when I want to see that saved record it does not display.
For example if I saves information for 2 employees. Employee number 1 and employee number 2.
And I want to see information about employee number 2 then what should I do my code that it works well for me?
Comment