C++ PROGRAMMING

DATA ENTRY OF EMPLOY IN C++

DATA ENTRY OF EMPLOY IN C++

EDITING THE FILE IN C++

To make change in the file , the file must be accessed at any position. there are two position associate with the file .the first position is the one where the code can write. the second position is the one where the code will read from c++ provides method to change this current pointer position and fetch the current pointer position.

 

Sample Program

#include<iostream.h>

#include<stdib.h>

c++ programming
technoedit-codeblock

#include<string.h>

#include<fstream.h>

#include<conio.h>

class employee

{

int empno;

char empname[30];

int deptno;

int age;

double salary;

public:

employee()

{}

int display_empno(int eno)

{

if(empno==eno)

{

return 1;

}

else

return0;

}

void display()

{

cout<<“\nEmployee Number : “<<empno;

cout<<“\nEmployee Number : “<<empname;

cout<<“\nDept No :”<<deptno;

cout<<“\nAge :”<<age;

cout<<“\nSalary :”<<salary;

cout<<“\n\n”;

}

void getdata()

{

cout<<“\nEnter Employee No :”;

cin>>empno;

cout<<“\nEnter Employee Name :”;

cout<<“\nEnter DeptNo :”;

cin>>deptno;

cout<<“\nEnter age:”;

cin>>age;

cout<<“\nEnter Salary :”;

cin>>salary;

}

};

void main()

{

fstream file;

file.open(“myemp.dat”, ios::in | ios::b in ary | ios::nocreate);

if(!file)

file.open(“m yemp.dat”, ios::in | ios::out | ios::binary);

employee emp;

int choice;

clrscr();

do

{

cout<<“\n1) Insert Recordd\n2)Search Record|n3\n3) Display All Records\n4) Exit\n”;

cin>>choice;

int empno;

int flag=0;

switch(choice)

case1                                                        emp.getdata();

file.seekp(0,ios::end);

file.write((char*) &emp,sizeof(emp));

cout<<“\nEmployee Dettails successfully stored”;

break;

case2:

cout<<“\nEnter the employee number to search :”;

cin>>empno;

file.seekg(0,ios::beg);

while(file.eof()==0)

{

file.read((char*) &emp,sizeof(emp));

flag= emp.display_empno(empno);

if(flag==1)

{

emp.display();

break;

}

}

if(flag==0)

cout<<“\nEmployee Number doesn’t exist\n”;

file.clear();

break;

case 3

int r;

file.seekg(0,iod::beg);

while(1)

{

file.read((char*) &emp,sizeof(emp));

if(!file)

break;

emp.display();

}

file.clear();

break;

case 4:

file.close();

exit(0);

default:

cout<<“\nInvalid Choice\n”;

}

}while(1);

}

 YOUR OUTPUT IS:-

SEARCH RECORD

Enter the employ no to search : 103

employ number doesn’t exist

  1. Insert record
  2. Search record
  3. Display all record
  4. Exit

Enter the the employ no to searchb: 106

Employ number :106

Employ name:varum

Deptno:30

Age:45

Salary:25000

NOW PRESS ENTER IT WILL SHOW YOUR RECORDS

  1. Employ name:varum
  2. Age:45
  3. Salary:25000
  4. Employ name:kuar
  5. Dept:13
  6. Age:45
  7. Salary:25000
  8. Employ name:kumar
  9. Dept:13
  10. Age:45
  11. Salary:25000

1)Insert Record

2)Search record

3)Display all records

4)Exit