Welcome to Dream.In.Code
Become a C++ Expert!

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!




File Reading and Writing

 
Reply to this topicStart new topic

File Reading and Writing, Write a program that reads the file Employee.dat as input...

idle_09
6 Jul, 2008 - 06:28 PM
Post #1

New D.I.C Head
*

Joined: 2 Mar, 2008
Posts: 17


My Contributions
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.

cpp
#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 << "

Mod Edit:
Please code.gif
Thanks, gabehabe smile.gif
User is offlineProfile CardPM
+Quote Post

realNoName
RE: File Reading And Writing
6 Jul, 2008 - 07:18 PM
Post #2

D.I.C Regular
***

Joined: 4 Dec, 2006
Posts: 300



Thanked: 5 times
My Contributions
1) you have a typo with your includes
#include <iostrean> -- should be --> #include <iostream>

2) you can only have 1 file per variable so you need something like this
CODE
    ofstream managers("Managers.dat", ios::app),
             marketing("Marketing.dat", ios::app),
             developers("Developers.dat", ios::app),
             testers("Testers.dat", ios::app);
* you will have to change your if statement

3) another typo with im_stream... -- should be --> in_stream...

4) you need to make an int variable to hold the department

5) you want to move your input inside your while loop
CODE
    while (!in_stream.eof())
    {
        in_stream >> first_name >> last_name >> dep;
        //do stuff
    }


6) your switch will look something like this (in problem 4 i made a int called dep)
CODE
        switch(dep)
        {
        case Managers:
            // do stuff
            break;
        case Marketing:
            // do other stuff
            break;
        }



User is offlineProfile CardPM
+Quote Post

idle_09
RE: File Reading And Writing
7 Jul, 2008 - 12:26 PM
Post #3

New D.I.C Head
*

Joined: 2 Mar, 2008
Posts: 17


My Contributions
This is UPDATED CODE. I am getting some SYNTEX ERRORS.
cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
string line;
ifstream in_stream;
ofstream out_stream;

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

ofstream("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;
int departmnet;
enum department { Managers = 1, Marketing = 2, Developers = 3, Testers = 4 };

in_stream >> first_name >> last_name;
while (!in_stream.eof())
{
in_stream >> first_name >> last_name;
switch (department)
{
department '1':
cout << first_name << last_name;
break;

department '2':
cout << first_name << last_name;
break;

department '3': //syntax error : missing ';' before ':'
cout << first_name << last_name;
break;

department '4':
cout << first_name << last_name;
}
}
return 0;
}

Mod Edit: (again) tongue.gif
Please code.gif
Thanks, gabehabe smile.gif
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: File Reading And Writing
7 Jul, 2008 - 12:39 PM
Post #4

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
The only real problem was with your switch. The syntax is:
case 1: instead of department '1':

cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
string line;
ifstream in_stream;
ofstream out_stream;

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

ofstream("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;
int department;
enum department { Managers = 1, Marketing = 2, Developers = 3, Testers = 4 };

in_stream >> first_name >> last_name;
while (!in_stream.eof())
{
in_stream >> first_name >> last_name;
switch (department)
{
case 1:
cout << first_name << last_name;
break;

case 2:
cout << first_name << last_name;
break;

case 3: //syntax error : missing ';' before ':'
cout << first_name << last_name;
break;

case 4:
cout << first_name << last_name;
}
}
return 0;
}

Hope this helps smile.gif
User is offlineProfile CardPM
+Quote Post

idle_09
RE: File Reading And Writing
7 Jul, 2008 - 01:41 PM
Post #5

New D.I.C Head
*

Joined: 2 Mar, 2008
Posts: 17


My Contributions
I am still getting 6 ERRORS:

cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
string line;
ifstream in_stream;
ofstream out_stream;

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

ofstream("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;
int departmnet;
enum department { Managers = 1, Marketing = 2, Developers = 3, Testers = 4 };

in_stream >> first_name >> last_name;
while (!in_stream.eof())
{
in_stream >> first_name >> last_name;
switch (department)//error C2059: syntax error : ')'
{//error : missing ';' before '{'
case 1://error: illegal case
cout << first_name << last_name;
break;

case 2://error: illegal case
cout << first_name << last_name;
break;

case 3://error: illegal case
cout << first_name << last_name;
break;

case 4://error: illegal case
cout << first_name << last_name;
}
}
return 0;
}

Mod Edit: (again)
Please code.gif
Thanks, gabehabe smile.gif
User is offlineProfile CardPM
+Quote Post

realNoName
RE: File Reading And Writing
7 Jul, 2008 - 05:48 PM
Post #6

D.I.C Regular
***

Joined: 4 Dec, 2006
Posts: 300



Thanked: 5 times
My Contributions
QUOTE(gabehabe @ 7 Jul, 2008 - 01:39 PM) *

The only real problem was with your switch. The syntax is:
case 1: instead of department '1':

Hope this helps smile.gif

Really, i hope you just didn't look at the code that much... maybe its just me but i dont see how thats going to work at all


Here is most of the code fixed you just need to add a little more to the switch
CODE
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

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;

                default:
                    cout << "Department not found" << endl;
        }
    }
    return 0;
}


This post has been edited by realNoName: 7 Jul, 2008 - 05:50 PM
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: File Reading And Writing
8 Jul, 2008 - 01:41 AM
Post #7

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
QUOTE(realNoName @ 8 Jul, 2008 - 02:48 AM) *
Really, i hope you just didn't look at the code that much... maybe its just me but i dont see how thats going to work at all
You're right, I didn't blush.gif I was doing 1,001 other things at the same time.

Sorry about that.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 12:57PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month