-2

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;

EDIT: ADDED SAMPLE TEXT FILE

First name:Test

Last name: asdfas

Employee no: 12

(etc.)

Local Contact: 12323

***********************************************

First name:Test2

Last name: asd

Employee no: 23432

(etc.)

Local Contact: 234324

***********************************************

void hr::empdetails()       
{
    //declaring all datamembers
        char firstname [30], lastname [30], dept[30]; //etc.


    ofstream outfile;
    outfile.open ("example.txt",ios::app);

        //inputting all details
        //writing details into text file...

        outfile<<"First name:";
        outfile<<firstname;

        //...................
        outfile<<"\nLocal Contact: ";
        outfile<<localcon;
    outfile<<"\n\n*************************************************";//indicating end of employee's details
}

void hr::searchname()
{
        //what should i write here to search for a name and display all of its details
}

Aeden Thomas
  • 63
  • 1
  • 5
  • Use `eof` but also use an `if` statement to display only the employee you are interested in instead of all the employees. – john Jun 29 '19 at 08:58
  • 1
    @john "_Use `eof`_" I wouldn't suggest this: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Algirdas Preidžius Jun 29 '19 at 09:04
  • provide sample file of employees.... – sha111 Jun 29 '19 at 09:41
  • @john how to do that? – Aeden Thomas Jun 29 '19 at 10:14
  • @sha111 have attached it now – Aeden Thomas Jun 29 '19 at 10:30
  • @AlgirdasPreidžius any other alternative? – Aeden Thomas Jun 29 '19 at 10:33
  • What specific problem are you facing? Try to narrow things down so that you can explain your issue with much less code to illustrate it. (For example, there should be no need for user input for your question; pick a search and hard code it into your question's code.) Simplify until you have only what is needed to reproduce your issue. – JaMiT Jun 29 '19 at 10:42
  • @JaMiT sorry for that. i am new to this :) – Aeden Thomas Jun 29 '19 at 10:59
  • 1
    @AedenThomas What exactly is the problem? Seems to me you have multiple problems even though you're only asking about one. The first problem would seem to be how to read in the sample file, you have to know how do to that before you can do any searching for particular names. If you can I would choose a simpler format to store your data in. – john Jun 29 '19 at 11:47
  • @john I am using ‘ifstream’ for reading. What format should I use? – Aeden Thomas Jun 29 '19 at 12:31
  • @AedenThomas Something as simple as possible, so that it's easy to read back. For instance I would drop all the labels `"First name:"` etc. and just have each employee on a single line with a suitable separator between each data field. That's not the only choice of course, the point is that you pick some format to make your life as easy as possible. – john Jun 30 '19 at 06:27

2 Answers2

0
int main()
{
    ifstream fin("look.txt");. // Here you have to provide file name

    string line; // takes a line at a time.

    int person = 1; // this increments person 

    while (getline(fin, line)) // here we are reading data line by line till eof
    {
        if (line == "***********************************************") // this is point where we increment the person variable by one ( to change person )
            person++;

        int ind = line.find_last_of(':'); // here we are finding ':' character to get fields name like First Name , Last Name ,etc..

        string cc = line.substr(0, ind); // here we get value of the fields ex:- First Name :-Sha11 ( here we fetch Sha11 .. you use this way to compare empolyees various value ,in your desired way.. )

        if (cc == "First name" || cc == "Last name" || cc == "Local Contact") ( It is looking only for some desired fields , but you might change this according to you. )
        {
            if (ind != string::npos) 
            {
                int diff = line.size() - ind - 1;

                string pa = line.substr(ind + 1, diff);
                cout << person << " : " << cc << " : " << pa << endl; // here cc stores the field's name and pa stores the field's value. here i used substr() and find() to get desired results from the string (for more details about these function look at these urls "www.cplusplus.com/reference/string/string/find/"  , "http://www.cplusplus.com/reference/string/string/substr/")..
            }
        }
    }

    return 0;
}

This commented explanation might help you...!

This might solve your problem....

JaMiT
  • 14,422
  • 4
  • 15
  • 31
sha111
  • 113
  • 1
  • 12
  • 1
    Rather than posting a solution, please explain *how* your code resolves the OP's question or issue. We prefer to have the OPs learn to write their own code rather than giving them code. How do we know your code works, without any explanation given? – Thomas Matthews Jun 29 '19 at 17:51
0

In most cases, the method is to read in all the fields in a record and only use the fields that you need. Reading the extra fields will not take any extra time versus executing code to skip over them.

Also, prefer arrays (std::vector) of structures to parallel arrays:

struct Employee_Record
{
  std::string first_name;
  std::string last_name;
  int id;
  //...
};
std::vector<Employee_Record> database;
Employee_Record array[32];

You can make the input simpler by overloading operator>> for the structure:

struct Employee_Record
{
  //...
  friend istream& operator>>(istream& input, Employee_Record& er);
};
istream& operator>>(istream& input, Employee_Record& er)
{
  getline(input, er.first_name);
  getline(input, er.last_name);
  //...
  return input;
}

You input code would look something like this:

std::vector<Employee_Record> database;
Employee_Record er;
while (data_file >> er)
{
  database.push_back(er);
}

A common technique is to read in all the data, then process the data (such as searching).

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154