I am making a program where I input the details of many employees and store it in a text file. How can I make a function to search the entire text file and display the only details of that employee and not anyone else's? The details is always being inputted in append mode. I can't use eof() as it will display the entire document.
This is a school project and we have only studied cin and cout and not std::, hence I am using using namespace std;
void hr::menu()
{
int option, searchopt;
//................
cout<<"2. Search Employee"<<endl; /* By empno, name, dept */
//.................
mainopt:
cout<<"\nChoose your option-";
cin>>option;
switch (option)
{
//................
case 2:
cout<<"1. Search by empno";
cout<<"\n2. Search by name";
cout<<"\n3. Search by dept";
search:
cout<<"\n\nChoose your search option-";
cin>>searchopt;
switch (searchopt)
{
case 1:
cout<<"\nEMPNO SEACRH\n";
empnosearch();
break;
case 2:
cout<<"\nNAME SEARCH\n";
namesearch();
break;
case 3:
cout<<"\nDEPARTMENT SEARCH\n";
deptsearch();
break;
default:
cout<<"Invalid Option. ";
goto search;
break;
}
break;
//................
}
void hr::newemp()
{
//declaring all datamembers
char firstname [30], lastname [30], dept[30]; //etc.
long localcon;
int day, month, year;
ofstream outfile;
outfile.open ("example.txt",ios::app);
//inputting all details
//....................etc.
//writing details into text file...
outfile<<"First name:";
outfile<<firstname;
outfile<<"\nLast name: ";
outfile<<lastname;
outfile<<"\nEmployee no: ";
outfile<<empnum;
//...................etc.
outfile<<"\nLocal Contact: ";
outfile<<localcon;
outfile<<"\n\n*************************************************";//indicating end of employee's details
}
Aucun commentaire:
Enregistrer un commentaire