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

Click For Summary
SUMMARY

This discussion focuses on creating a C++ program to parse employee data from a file named Employee.dat and output the data into four separate files: Managers.dat, Marketing.dat, Developers.dat, and Testers.dat. The program utilizes file streams to read from the input file and write to the output files based on the department number associated with each employee. Key issues addressed include the need for separate file streams for each output file and the correct implementation of a switch statement to direct employees to their respective files based on department enumeration.

PREREQUISITES
  • C++ programming fundamentals
  • File I/O operations in C++
  • Understanding of enumerations in C++
  • Basic error handling in C++
NEXT STEPS
  • Study C++ file handling techniques using ifstream and ofstream
  • Learn about C++ enumerations and their applications
  • Explore error handling best practices in C++ file operations
  • Investigate advanced string parsing techniques in C++
USEFUL FOR

Developers, particularly those working with C++ who need to implement file parsing and data organization based on specific criteria, such as department categorization in employee management systems.

idle_09
Messages
6
Reaction score
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())
    {
        count << "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())
    {
        count << "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
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::count << "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::count << "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::count << "Invalid department";
            break;
        }
    }
    in_stream.close();

    out_streamMgr.close();
    out_streamMkt.close();
    out_streamDev.close();
    out_streamTest.close();
}
 
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.
 

Similar threads

  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 10 ·
Replies
10
Views
26K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
4
Views
3K