File reading and writing

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

I tried to write the program but I don't know what to put in "SWITCH" loop. Here is what i have so far.

#include <iostream>
#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 << "

Hi. The tutorial has info on switch on the control structures page
http://www.cplusplus.com/doc/tutorial/control.html
and File Output here
http://www.cplusplus.com/doc/tutorial/files.html

With writing the files you have a choice between 1 output stream which you open and close to switch between files, or 4 output streams (1 for each file).
Having 4 streeams will be quicker and simpler to code, but take more resources (probably not an issue here).
In the switch statement simply have a case for each department and output the name to the appropriate stream in each case.
Remeber to include a break at the end of each case otherwise the code will run on to the next case.
Hello,

7thline from the buttom:
I think it should be "in_stream ..." insteead of "im_stream ..."

You have a typo there.

int main
Last edited on
Hi! Thanks for helping. Can you(Faldrax) give me one example for switch statement because i don't know if i should use cout or fout
Try something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Stram declaration for managers - need one for each output file
ofstream o_managers;
o_managers.open("Managers.dat", ios::app); 
...
enum department { Managers = 1, Marketing = 2, Developers = 3, Testers = 4 };
department dept;  //Need variable to hold value of enumeration;
while (!in_stream.eof())
{
   in_stream >> first_name >> last_name >> dept;  
   switch (dept)
   {
      case Managers: 
         o_managers << first_name << " " << last_Name << "\n";
         break;
      case  Marketing:
         ...
   }           
}

Note: Not sure if you can read directly into an enumeration, you may need to read as an int and convert.
Topic archived. No new replies allowed.