How to Parse and Output Employee Data from a File Based on Department

In summary, the program needs to open five files, each of which must be opened separately, and then iterate through the data in the Employee.dat file and send the appropriate output to the appropriate files.
  • #1
idle_09
6
0
Hi! Thanks for helping me. I need to write a program (in C++) that reads the file Employee.dat as input and then outputs to 4 different files which are Managers.dat, Marketing.dat, Developers.dat, and Testers.dat. Each of the output files contains the name of the employees of its category.

The fle Employee.dat contains following data:

Sue Leon 4
Robert Wise 3
Sam Woo 1
Nathan White 3
Suzan Head 2
Henry Williams 4
Christine Mint 1
Kim Leeds 4
Elton Sue 3
Ken Latch 2
Mentor note: The number associated with each employee is apparently his or her department. The enumeration in the code shows this.

I tried to write the program but I don't know what to put in "SWITCH" loop. Here is what i have so far.
C:
#include <iostrean>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
    ifstream in_stream;
    ofstream out_stream;

    in_stream.open("Employee.dat");
    if (in_stream.fail())
    {
        cout << "Input file failed to open.\n";
        exit(1);
    }

    out_stream.open(("Managers.dat", ios::app), ("Marketing.dat", ios::app), ("Developers.dat", ios::app), ("Testers.dat", ios::app);
    if (out_stream.fail())
    {
        cout << "Output file failed to open.\n";
        exit(1);
    }

    string first_name, last_name;
    enum department { Managers = 1, Marketing = 2, Developers = 3, Testers = 4 };

    im_stream >> first_name >> last_name >> department;
    while (!in_stream.eof())
    {
        switch (department)
        {
            number '1';
            out_stream << "
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
The snippet in the OP is a fair start, but the main problems are that 1) you need a file stream for each of the five files the program is dealing with, and 2) each file stream must be opened with a separate call to the open() function.

Here's my solution. I don't have any comments, but the code should be pretty much self-explanatory. The basic algorithm is to iterate through the input file, and send output to the appropriate output file, based on the department the current employee belongs to.
C:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

enum department { Managers = 1, Marketing = 2, Developers = 3, Testers = 4 };

int main()
{
    std::ifstream in_stream;
    std::ofstream out_streamMgr, out_streamMkt, out_streamDev, out_streamTest;

    in_stream.open("Employees.dat");
    if (in_stream.fail())
    {
        std::cout << "Input file failed to open.\n";
        exit(1);
    }

    out_streamMgr.open("Managers.dat");
    out_streamMkt.open("Marketing.dat");
    out_streamDev.open("Developers.dat");
    out_streamTest.open("Testers.dat");

    if (out_streamMgr.fail() || out_streamMkt.fail() ||
        out_streamDev.fail() || out_streamTest.fail())
    {
        std::cout << "Output file failed to open.\n";
        exit(2);
    }

    std::string first_name, last_name;
    int deptVal;

    while (!in_stream.eof())
    {
        in_stream >> first_name >> last_name >> deptVal;
        switch (deptVal)
        {
        case Managers: out_streamMgr << first_name << ' ' << last_name << ' ' << Managers << std::endl;
            break;
        case Marketing: out_streamMkt << first_name << ' ' << last_name << ' ' << Marketing << std::endl;
            break;
        case Developers: out_streamDev << first_name << ' ' << last_name << ' ' << Developers << std::endl;
            break;
        case Testers: out_streamTest << first_name << ' ' << last_name << ' ' << Testers << std::endl;
            break;
        default: std::cout << "Invalid department";
            break;
        }
    }
    in_stream.close();

    out_streamMgr.close();
    out_streamMkt.close();
    out_streamDev.close();
    out_streamTest.close();
}
 
  • #3
Might be helpful to tell the user which output file(s) failed to open.

I would naturally parse each line as a single string followed by an integer, since otherwise the logic fails when you need to distinguish between John Adam Doe and John Brian Doe or Anna Maria von Bonn joins the organisation.
 

1. What is file reading and writing?

File reading and writing refers to the process of accessing and manipulating data in a file. This includes reading data from a file and writing data to a file.

2. Why is file reading and writing important?

File reading and writing is important because it allows us to store and retrieve data, which is essential for many applications. It also allows for data to be shared between different programs and devices.

3. What are the different methods for file reading and writing?

There are several methods for file reading and writing, including sequential access and random access. Sequential access reads data from a file in a linear manner, while random access allows for direct access to specific data within a file.

4. How do you read data from a file?

To read data from a file, you first need to open the file using a file reading function. Then, you can use methods like read() or readline() to read the data from the file and store it in a variable for further use.

5. How do you write data to a file?

To write data to a file, you first need to open the file using a file writing function. Then, you can use methods like write() or writelines() to add data to the file. Don't forget to close the file after writing to save the changes.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top