this code is running but not storing data about employees in file. Please help in finding error in this program Code: //Create the class employee that contain detail of employee. #include<fstream.h> #include<string.h> #include<stdio.h> #include<conio.h> class employee { private: char ename[20]; char add[30]; float basic; int hra,da; int net_sal; public: char emp_no[7]; void getit() { cout<<"\nEmployee's name :"; gets(ename); cout<<"\nAddress :"; gets(add); cout<<"\nBasic salary :"; cin>>basic; hra=(basic*20)/100; da=(basic*10)/100; net_sal=basic+da+hra; } void showit() { cout<<"\nEmployee no. :"<<emp_no<<"\n"; cout<<"\nEmployee's name :"<<ename<<"\n"; cout<<"\nAddress :"<<add<<"\n"; cout<<"\nBasic salary :"<<basic<<"\n"; cout<<"\nHra :"<<hra<<"\n"; cout<<"\nDa :"<<da<<"\n"; cout<<"\nNet salary :"<<net_sal<<"\n"; } void search(employee&); void modify(employee&); }; void employee::search(employee &ep) { clrscr(); int flag,ch; ifstream in; in.open("emp.dat",ios::in); cout<<"\n******** 1: Search by name ********\n"; cout<<"\n******** 2: Search by empno. *******\n"; cout<<"\n\nEnter your choice :"; cin>>ch; switch(ch) { case 1: clrscr(); flag=0; char nm[20]; cout<<"\nEnter the name to be searched :"; gets(nm); in.read((char *)&ep,sizeof(ep)); while(in) { if(strcmp(ename,nm)==0) { ep.showit(); flag=1; } in.read((char *)&ep,sizeof(ep)); } if(flag==0) { cout<<"\nRecord not found \n"; } getch(); break; case 2: clrscr(); flag=0; char num[7]; cout<<"\nEnter the empno. to be searched :"; cin>>num; in.read((char *)&ep,sizeof(ep)); while(in) { if(strcmp(emp_no,num)==0) { ep.showit(); flag=1; } in.read((char *)&ep,sizeof(ep)); } if(flag==0) { cout<<"\nRecord not found \n"; } getch(); break; default : cout<<"\nYou have entered wrong choice \n"; } } void employee::modify(employee &ep) { clrscr(); int flag,n,s,ch; char num[7]; fstream fl; fl.open("emp.dat",ios::in|ios::out); cout<<"\n******** 1: Update record of specific name **********\n"; cout<<"\n******** 2: Update record after specific name *******\n"; cout<<"\n\nEnter your choice :"; cin>>ch; switch(ch) { case 1: clrscr(); flag=0; cout<<"\nEnter the name to be searched :"; cin>>num; fl.read((char *)&ep,sizeof(ep)); while(fl) { if(strcmp(emp_no,num)==0) { n=fl.tellg(); flag=1; break; } fl.read((char *)&ep,sizeof(ep)); } s=sizeof(ep); if(flag==1) { fl.seekp(n-s); ep.getit(); fl.write((char *)&ep,sizeof(ep)); } if(flag==0) { cout<<"\nRecord not found \n"; } break; case 2: clrscr(); flag=0; cout<<"\nEnter the name to be searched :"; cin>>num; fl.read((char *)&ep,sizeof(ep)); while(fl) { if(strcmp(emp_no,num)==0) { n=fl.tellg(); flag=1; break; } fl.read((char *)&ep,sizeof(ep)); } if(flag==1) { fl.seekp(n); ep.getit(); fl.write((char *)&ep,sizeof(ep)); } if(flag==0) { cout<<"\nRecord not found \n"; } break; default: cout<<"\nYou have entered wrong choice \n"; } } void main() { employee emp; fstream file; char ch; int n; file.open("emp.dat",ios::in|ios::out|ios::app); do { clrscr(); cout<<"\n******************* Select the following ***********************\n"; cout<<"\n******************* 1: Add record in file **********************\n"; cout<<"\n***************** 2: Search record in file *********************\n"; cout<<"\n***************** 3: Update record in file *********************\n"; cout<<"\n***************** 4: Display record in file ********************\n"; cout<<"\nEnter your choice :"; cin>>n; switch(n) { case 1: clrscr(); char eno[7]; x: cout<<"\nEmployee number :"; cin>>eno; file.read((char *)&emp,sizeof(emp)); while(file) { if(strcmp(emp.emp_no,eno)==0) { cout<<"\nEmployee number already exist \n"; goto x; } file.read((char *)&emp,sizeof(emp)); } strcpy(emp.emp_no,eno); emp.getit(); file.write((char *)&emp,sizeof(emp)); break; case 2: emp.search(emp); break; case 3: emp.modify(emp); break; case 4: clrscr(); file.seekg(0); file.read((char *)&emp,sizeof(emp)); while(file) { emp.showit(); file.read((char *)&emp,sizeof(emp)); } getch(); break; default: cout<<"\nYou have entered wrong choice \n"; } cout<<"\n\nDo you want to continue (y/n) :"; cin>>ch; }while(ch!='n'&&ch!='N'); getch(); }
You have a number of potential problems. For instance, you aren't testing to see if the file opened. Neither are you testing your various input functions for success. Those things are NOT guaranteed to work. They will either work OR inform you of failure. You need to ask. Further, do not use "gets". It is suceptible to buffer overrun. I would normally recommend fgets as a replacement, but this is C++. Use a C++ function. Also, when you use something like "cin >> ch;" you will leave things in the buffer. If you don't handle this properly, subsequent input will not work properly. I would also suggest that you use the string class instead of C strings. You'll have fewer problems. While this is not a requirement, I highly recommend that you set your tabs to something like 4 spaces and instruct your editor to replace tabs with spaces. Your code is moving off to the right 4,000 miles for no good reason; it detracts from readibility, not adds to it. Having to scroll horizontally just sucks.