Question is:
Create a class Employee with following constraints in mind:

  1. Declare three private data members named id,name,NIC of int,string,long respectively.
  2. Overload stream insertion and stream extraction operators.

Implement a global function named saveData, that'll allow the user to enter any number of employee's data and store it into a file named "edata.dat".
Implement a global function named readData, that'll read all the contents of "edata.dat" file and display them on console.

I have tried this one but confused a little bit how to arrange my code...
class Employee..

#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib> 
#include<iomanip>

using namespace std;

enum choices{write=1,read,end};
void saveData(fstream&);
void readData(fstream&);
void outputLine(int , const string, long); 

class Employee
{
    private:
        int ID;
        string name;
        long NIC;
    public:

        Employee ( int I_D, string n, long N_I_C) //parameterized constructor
        {
            ID=I_D;
            name=n;
            NIC=N_I_C;
        } //end constructor

        int enterChoice()
        {
            int menuChoice;

            cout<<"1. Write data in file. \n"<<
                  "2. Read Data from file. \n"<<
                  "3. Exit. \n";
            cout<<"enter your choice: "<<endl;

            do //input user's request
            {
                cin>>menuChoice;
            } while(menuChoice<write && menuChoice>end);

            return menuChoice;
        } //end function enterChoice

        implementation(int getChoice)
        {
            while(menuChoice!=end)
            {
            switch(getChoice)
             { 
                case write:
                    saveData(xyz);
                    break;
                case read:
                    readData(xyz);
                    break;
             } //end switch
            } //end while
        } //end function implemenetation

        //prototypes for overloaded operator ...
        friend ostream &operator<<(ostream &, const Employee &); //overloading stream insertion operator 
        friend istream &operator>>(istream &, Employee &); //overloading stream extraction operator
}; //end class Employee

Definition of overloaded operators:

istream &operator>>( istream &input,Employee &emp)
{
    input>>emp.ID>>' '>>emp.name>>' '>>emp.NIC;
    return input;
} //end function

ostream &operator<<(ostream &output, const Employee &emp)
{
    output<<left<<setw(15)<<"employee's ID"<<right<<setw(20)<<"Name"<<right<<setw(15)<<"NIC"<<endl;
    output<<left<<setw(15)<<emp.ID<<right<<setw(20)<<emp.name<<right <<setw(15)<<emp.NIC<<endl;
    return output;
} //end function

For writing data in file:

//writing data in file
void saveData(fstream &insertInFile)
{
    ofstream abc("edata.txt",ios::out);

    //exits program if file is not found
    if(!abc)
    {
        cerr<<"file not found."<<endl;
        exit(1);
    } //end if

    cout<<"enter end-of-file to end input."<<endl;

    int id;
    string Name;
    long nic;

    while(cin>>id>>Name>>nic)
    {
        abc<<id<<' '<<Name<<' '<<nic<<endl;
        cout<<"?";
    } //end while 
} //end saveData

For reading data from file:

void readData(fstream &read4rmFile)
{
    ifstream def("edata.dat",ios::in);

    //exits if file is not found
    if(!def)
    {
        cerr<<"file is not found."<<endl;
        exit(1);
    } //ends if

    int id;
    string Name;
    long nic;

    read4rmFile<<left<<setw(15)<<"employee's ID"<<right<<setw(20)<<"Name"<<right<<setw(15)<<"NIC"<<endl;
    while(!read4rmFile.eof())
    outputLine(id, Name,nic);
} //end readData

And finally the main and the global function:

int main()
{
    Employee obj;

    fstream xyz("edata.dat", ios::in| ios::out ); 

    int number=obj.enterChoice();
    obj.Employee(number);

    return 0; 
} //ends main

void outputLine(int a,const string b, long c)
{
    cout<<left<<setw(15)<<a<<right<<setw(20)<<b<<right<<setw(15)<<c<<endl;
} //end outputLine

Now my confusion is the function readData isn't working properly and where to put the overloaded functions?

I don't see any code in the function readData that reads from the file edata.dat

Start by writing a function that opens and reads data from edata.dat

If you were to google C++ file read / write you could find many code examples and articles to help ... ( also re. overloaded << and overloaded >> ) ...

Here is a link with several file read / write examples ... You might like to take a look at the BP (Blood Pressure) file read example especially:

http://developers-heaven.net/forum/index.php/topic,2019.0.html

Thanks David. Let me take a deep look at this link :)

Actually I have a question in my mind. Does the compiler makes the file itself we mention in the statement,or we have to create it first? like:

const string MY_FILE = "BP.txt";

And other thing in your this Blood pressure example code is that You didn't write any lines to read from file or write data in file but still it is working. How?

I excetued that example on my complier and it is just giving the output like:
Press 'Enter' to continue/exit ...

Why is it not taking any data from user or something else?

And what is the purpose of this line?

ifstream fin( MY_FILE.c_str() ); // C++ string gets converted to C string

ifstream fin( MY_FILE.c_str() );

Create a new object of type ifstream.
This new object is to be named fin.
Use the default constructor that takes a single parameter of type char* to do it.
Get that parameter by calling the function MY_FILE.c_str()

It also means that this code is many years out of date. Since C++11, there is an ifstream constructor that simply takes an object of type std::string

It also means that this code is many years out of date. Since C++11, there is an ifstream constructor that simply takes an object of type std::string

Unfortunately, it seems, many beginning students are still being asked to code to a C++ standard that is before C++11 ... so that their code can be compiled ok on standard C++ compilers pre C++11 ... but good to point out some of the nice things added ... in C++11 and C++14 versions ... as one feels may be appropriate to a beginning student.

Actually I have a question in my mind. Does the compiler makes the file itself we mention in the statement,or we have to create it first? like:
const string MY_FILE = "BP.txt";

You could use a text editor (copy/paste using an editor like Windows notepad) to make the test file I called "BP.txt"

And using save as ... save those data lines as BP.txt (in the same directory/folder as the executable file generated by your compiler.)

Make sure the file name is NOT saved as BP.txt.txt ... (if it is, rename it.)
Recall the example program is looking for a file with the name of BP.txt

What to copy paste in Windows Notepad to make the test file?

You seem to have found the BP (Blood Presure) example program ok at the link I provided above ...

But ... note HINT!

If you read the (embedded) comments in that example program ...

// data file is structured like this ...
/*
Smith Jane 111/76
Jones Rob 98/70
Costello Judy-Ann 144/90
Frank-Anderson Bibby-Bonnie 190/30
Sue Peggy 10/5
James-Thomas Andrew 190/111
*/
const string MY_FILE = "BP.txt";

you would see a suggested starting set of 'test data'

that you might 'copy/paste into a file you call BP.txt

ALSO ...

please note the 'fix' code added below the exanmple to use instead of the code in the example.

i.e.

instead of :

// define overloaded >> for input of BP objects ...
friend istream& operator >> ( istream& is, BP& p )
{
    char skip;
    string scrap;
    is >> p.lname >> p.fname >> p.sys >> skip >> p.dia;
    getline( is, scrap );
    return is;
}

use this 'suggested fix' :

// define overloaded >> for input of BP objects ...
friend istream& operator >> ( istream& is, BP& p )
{
    string line;
    getline( is, line );
    istringstream iss( line ); // construct iss from line ...
    char skip;
    iss >> p.lname >> p.fname >> p.sys >> skip >> p.dia;
    return is;
}

And instead of just these includes :

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

Have this :

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stream> // re. istringstream object

Okay thanks I will try it :)

Opps ... (typo fixed)

And instead of these includes :

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

Have these :

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream> // re. istringstream object

Wao it worked.It is just reading that stored text from file. What if I want to enter the name of patient and BP(Blood Pressure) myself... Now what should I do?

See what you can code to take in data from a keyboard user (with suitable prompts) that could replace the code that takes in data from a data file.

Using the concept of File Handling and Operator Overloading, create a class Addition in which numbers are added through Operator Overloading technique. The numbers are saved in two different .txt file.

Make two .txt files and save a integer in it. Files can be created in the code.

While making class get integer value from .txt files

Perform addition of two by using operator overloading technique

PLZ ANY BODY CAN MAKE THIS PROGRAM PLZZ ITS ARGENT

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.