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

AI Thread Summary
The discussion focuses on creating a C++ program that reads employee data from a file named Employee.dat and outputs the names of employees categorized by their departments into four separate files: Managers.dat, Marketing.dat, Developers.dat, and Testers.dat. The Employee.dat file contains employee names followed by a number indicating their department. Key points include the need for separate file streams for each output file, as opposed to a single stream for all, and the correct implementation of a switch statement to direct employees to the appropriate output file based on their department number. The initial code provided has issues, such as incorrect file opening syntax and a misunderstanding of how to handle input data. The suggested solution emphasizes iterating through the input file, reading each employee's name and department, and using the switch statement to write the names to the correct output files. Additionally, it is recommended to handle potential errors in file opening more explicitly to improve user feedback.
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())
    {
        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
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();
}
 
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top