Join 137,208 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,321 people online right now. Registration is fast and FREE... Join Now!
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.
int main() { // this variable is not used //string line;
ifstream in_stream;
// dont need this... you have to have a variable for each file //ofstream out_stream;
in_stream.open("Employee.dat"); if (in_stream.fail()) { cout << "Input file failed to open.\n"; exit(1); }
/* So whats are the variable names? how do you use them if they dont have names? ofstream("Managers.dat", ios::app), ("Marketing.dat", ios::app), ("Developers.dat", ios::app), ("Testers.dat", ios::app); */
/* All you had to do was copy my code what this does is make 4 variables (managers,marketing,developers,testers) for the file handling */ ofstream managers("Managers.dat", ios::app), marketing("Marketing.dat", ios::app), developers("Developers.dat", ios::app), testers("Testers.dat", ios::app);
// if one of the files didnt open then exit if (managers.fail() || marketing.fail() || developers.fail() || testers.fail()) { cout << "Output file failed to open.\n"; exit(1); }
string first_name, last_name;
//int departmnet; // dont know if this is a typo or not int dep; enum department { Managers = 1, Marketing = 2, Developers = 3, Testers = 4 };
//if you keep this here the first person in your employee's file would never get sorted //in_stream >> first_name >> last_name; while (!in_stream.eof()) { in_stream >> first_name >> last_name >> dep; switch (dep) { // you could use case 1: case Managers: // send first_name and last_name to the managers stream aka Managers.dat managers << first_name << ' ' << last_name << endl; break;
// you could use case 2: case Marketing: marketing << first_name << ' ' << last_name << endl; break;