Hi,
I have a big problem with my program..I have not completed all its functions but the ones I have included the whole program below..the program is basically an employee payroll program which has 2 files, 1 which holds staff details such as employee name and number,address etc. The other file holds that employee's corresponding salary details such as allowances and deductions for tax etc. Both the structures have 3 elemnts common to both. The thing is, I'm not even sure the file is being written into, I have a display function which is supposed to display an employee's details when the user enters his employee number..ANY help is appreciated!
Code:
#include<fstream.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
#include<dos.h>
#include<process.h>
//GLOBAL VARIABLES..........................................................................................................
int n;//global variable n to store number of records
//FUNCTION PROTOTYPES.......................................................................................................
void main();
void append();
void display();
void modify();
//STRUCTURES FOR STAFF FILE AND STAFF SALARY FILE...........................................................................
//Staff structure
struct staff
{
int empno;
char na[20];
char add[20];
long tp;
int dj;
int dd;
char desig[20];
char dept[20];
long double bsal;
}z;
//Salary structure
struct sal
{
int empno;
long double bsal;
long double da;
long double hra;
long double ca;
long double medical;
long double pf;
long double itax;
long double gsal;
long double netsal;
}y;
//FUNCTIONS.................................................................................................................
//MAIN FUNCTIONS(TO BE LOADED FROM MAIN FUNCTION)
//Create file function to enter records for both salary and staff files
void create()
{
clrscr();
long count;//variable used to time delay loop
cout<<"\nHow many records do you wish to create?\n";
cin>>n;
ofstream f1,f2;
f1.open("staff.dat",ios::out|ios::binary);
f2.open("salary.dat",ios::out|ios::binary);
for(int i=0;i<n;i++)
{
cout<<"\nEnter employee number\n";
cin>>z.empno;
cout<<"Enter name of employee\n";
gets(z.na);
cout<<"Enter Phone number of employee\n";
cin>>z.tp;
cout<<"Enter address of employee\n";
gets(z.add);
cout<<"Enter date of joining of employee\n";
cin>>z.dj;
cout<<"Enter date of birth of employee\n";
cin>>z.dd;
cout<<"Enter designation of employee\n";
gets(z.desig);
cout<<"Enter department\n";
gets(z.dept);
cout<<"Enter basic salary\n";
cin>>z.bsal;
cout<<"\nWriting Staff file.....\n";
f1.write((char*)&z,sizeof(z));
cout<<"\nFile successfully created!\n";
cout<<"\n\nEnter information for Staff Salary file\n";
cout<<"Enter medical allowance\n";
cin>>y.medical;
y.empno=z.empno;
y.bsal=z.bsal;
y.hra=z.bsal*0.15;
y.da=z.bsal*0.3;
y.pf=z.bsal*0.08;
y.ca=z.bsal*0.1;
y.gsal=z.bsal+y.da+y.hra+y.ca;
y.itax=y.gsal*0.3;
y.netsal=y.gsal-(y.pf+y.itax);
f2.write((char*)&y,sizeof(y));
cout<<"\nWriting Staff salary file\n";
cout<<"File successfully created!\n";
}
cout<<"Returning to Main menu.......\n";
f1,f2.close();
for(count=0;count<=90000;count++);
main();
}//create()
//SUB FUNCTIONS(WITHIN MENUS).................................................
//STAFF FILE FUNCTIONS........................................................
//Display the staff menu with corresponding functions
void staff()
{
clrscr();
cout<<"\nSTAFF MENU\n";
cout<<"==========\n";
cout<<"1.APPEND N NUMBER OF RECORDS\n";
cout<<"2.DISPLAY AN EMPLOYEE'S RECORD\n";
cout<<"3.MODIFY AN EMPLOYEE'S RECORD\n";
int choice;
cout<<"Enter choice\n";
cin>>choice;
switch(choice)
{
case 1:cout<<"\nAPPEND FUNCTION\n";
append();
break;
case 2:cout<<"\nDISPLAY RECORD\n";
display();
break;
case 3:cout<<"\nMODIFY THE RECORD OF AN EMPLOYEE\n";
modify();
break;
default:cout<<"\nWrong choice entered!Returning to main menu...\n";
main();
break;
}//switch
}//staff function
//Append function to append records to staff and salary files
void append()
{
clrscr();
ofstream f1,f2;
int i;
long count;
f1.open("staff.dat",ios::out|ios::app);
f2.open("salary.dat",ios::out|ios::app);
int no;
cout<<"\n\t\tAPPEND FUNCTION\n";
cout<<"\t\t===============\n";
cout<<"Enter number of records to be appended\n";
cin>>no;
for(i=0;i<no;i++)
{
cout<<"\nEnter employee number\n";
cin>>z.empno;
cout<<"Enter name of employee\n";
gets(z.na);
cout<<"Enter Phone number of employee\n";
cin>>z.tp;
cout<<"Enter address of employee\n";
gets(z.add);
cout<<"Enter date of joining of employee\n";
cin>>z.dj;
cout<<"Enter date of birth of employee\n";
cin>>z.dd;
cout<<"Enter designation of employee\n";
gets(z.desig);
cout<<"Enter department\n";
gets(z.dept);
cout<<"Enter basic salary\n";
cin>>z.bsal;
cout<<"\nAppending to Staff file.....\n";
f1.write((char *)&z,sizeof(z));
cout<<"\nRecord(s) successfully appended!\n";
cout<<"\n\nEnter information for Staff Salary file\n";
cout<<"Enter medical allowance\n";
cin>>y.medical;
y.empno=z.empno;
y.bsal=z.bsal;
y.hra=z.bsal*0.15;
y.da=z.bsal*0.3;
y.pf=z.bsal*0.08;
y.ca=z.bsal*0.1;
y.gsal=z.bsal+y.da+y.hra+y.ca;
y.itax=y.gsal*0.3;
y.netsal=y.gsal-(y.pf+y.itax);
f2.write((char*)&y,sizeof(y));
cout<<"\nAppending Staff salary file\n";
cout<<"Record(s) successfully appended!\n";
cout<<"Returning to Main menu.......\n";
}
f1,f2.close();
for(count=0;count<=10000;count++);
for(count=0;count<=10000;count++);
main();
}//append
//Display an employee's record according to employee number
void display()
{
clrscr();
int flag=0;
int eno;
int nr;
cout<<"\n\t\tDISPLAY FUNCTION\n";
cout<<"\t\t================\n";
ifstream f1,f2;
f1.open("staff.dat",ios::in|ios::binary);
f2.open("salary.dat",ios::in|ios::binary);
cout<<"\nEnter employee number to be searched for\n";
cin>>eno;
f1.read((char*)&z,sizeof(z));
cout<<z.empno;
cout<<eno;
getch();
while(f1)
{
getch();
if(z.empno==eno)
{
float l=f1.tellg();//no of bytes read by f1 stream
nr=l/sizeof(z);//no of records read
flag=1;
break;
}//if
f1.read((char*)&z,sizeof(z));
}//while
if(flag==1)
{
f2.seekg(0);
cout<<"\nEMPLOYEE NUMBER : "<<z.empno;
cout<<"\nEMPLOYEE NAME : "<<z.na;
cout<<"\nEMPLOYEE ADDRESS : "<<z.add;
cout<<"\nTELEPHONE NUMBER : "<<z.tp;
cout<<"\nDATE OF JOINING : "<<z.dj;
cout<<"\nDATE OF BIRTH : "<<z.dd;
cout<<"\nDESIGNATION : "<<z.desig;
cout<<"\nDEPARTMENT : "<<z.dept;
cout<<"\nBASIC SALARY : "<<z.bsal;
cout<<"\nALLOWANCE";
f2.seekg(nr*sizeof(y));//to get the read pointer to correspoding y location
cout<<"\nDA : "<<y.da;
cout<<"\nHRA : "<<y.hra;
cout<<"\nCONVEYANCE : "<<y.ca;
cout<<"\nMEDICAL : "<<y.medical;
cout<<"\nPROVIDENT FUND : "<<y.pf;
cout<<"\nINCOME TAX : "<<y.itax;
cout<<"\nGROSS SALARY : "<<y.gsal;
cout<<"\nNET SALARY : "<<y.netsal;
getch();
}//if
else
cout<<"\nCould not find a matching employee number\n";
f1,f2.close();
main();
}//display
//Modify an employee's record
void modify()
{
cout<<"\n\t\tMODIFY RECORD FUNCTION\n";
cout<<"\t\t======================\n";
int rno;
cout<<"Enter record number to modify\n";
cin>>rno;
ofstream f1,f2;
f1.open("STAFF.JRH",ios::out|ios::binary|ios::ate);
f2.open("SAL.JRH",ios::out|ios::binary|ios::ate);
f1.seekp((rno-1)*sizeof(z));
cout<<"\nEnter employee number\n";
cin>>z.empno;
cout<<"Enter name of employee\n";
gets(z.na);
cout<<"Enter Phone number of employee\n";
cin>>z.tp;
cout<<"Enter address of employee\n";
gets(z.add);
cout<<"Enter date of joining of employee\n";
cin>>z.dj;
cout<<"Enter date of birth of employee\n";
cin>>z.dd;
cout<<"Enter designation of employee\n";
gets(z.desig);
cout<<"Enter department\n";
gets(z.dept);
cout<<"Enter basic salary\n";
cin>>z.bsal;
cout<<"\nModifying Employee Record\n";
f1.write((char*)&z,sizeof(z));
cout<<"File successfully modified!\n";
cout<<"Enter information for Staff Salary file\n";
cout<<"Enter medical allowance\n";
cin>>y.medical;
y.empno=z.empno;
y.bsal=z.bsal;
y.hra=z.bsal*0.15;
y.da=z.bsal*0.3;
y.pf=z.bsal*0.08;
y.ca=z.bsal*0.1;
y.gsal=z.bsal+y.da+y.hra+y.ca;
y.itax=y.gsal*0.3;
y.netsal=y.gsal-(y.pf+y.itax);
f2.write((char*)&y,sizeof(y));
cout<<"Modifying staff salary of employee\n";
cout<<"File successfully modified!\n";
main();
}//modify
//MAIN FUNCTION.............................................................................................................
void main()
{
clrscr();
int choice;
cout<<"\nMAIN MENU\n";
cout<<"=========\n";
cout<<"1.CREATE FILE\n";
cout<<"2.STAFF FILE\n";
cout<<"3.GENERAL REPORT\n";
cout<<"4.SALARY FILE\n";
cout<<"5.QUIT\n";
cout<<"Enter choice\n";
cin>>choice;
switch(choice)
{
case 1: cout<<"CREATE FILE\n";
create();
break;
case 2: cout<<"STAFF FILE\n";
staff();
break;
case 3: cout<<"GENERAL REPORT\n";
break;
case 4: cout<<"SALARY FILE\n";
break;
case 5: cout<<"QUIT\n";
exit(0);
break;
default:cout<<"Wrong choice entered...\n";
main();
}//switch
}//main function