I have got the program writing to the files, but i am unsure where to even begin to pull the information back out.
the information is saved like this:

15 Ray Allrich # 10.25 0 0
in one file the other file has the id number and hours worked
5 40

Any help would be greatly appreciated.

      const int NUM_EMPS = 6;
     struct Pay
     { double OverTime;
        double GrossPay;
        double NetPay;
        double Tax;
        double TaxRate = 0.15;
        double TotalNet;
        double TotalGross;
        double DepCost = 20.00;
        int count;
        int Hours;

     };



    class Employee 
    {
      private:
        int id;             // employee ID
        string name;        // employee name
        double hourlyPay;   // pay per hour
        int numDeps;        // number of dependents
        int type;           // employee type



      public:
        string getName();
        double getHrpay();
        int getDeps();
        int getType();
        int getId();


        Employee( int initId=0, string initName="", 
                  double initHourlyPay=0.0, 
                  int initNumDeps=0, int initType=0 );  // Constructor

        bool set(int newId, char newName[], double newHourlyPay,
                 int newNumDeps, int newType);

    };

    Employee::Employee( int initId, char initName[], 
                        double initHourlyPay,
                        int initNumDeps, int initType )
    {
      bool status = set( initId, initName[], initHourlyPay, 
                         initNumDeps, initType );

      if ( !status )
      {
        id = 0;
        name = " ";
        hourlyPay = 0.0;
        numDeps = 0;
        type = 0;    
      }
    }

    bool Employee::set( int newId, string newName, double newHourlyPay,
                                     int newNumDeps, int newType )
    {
      bool status = false;

      if ( newId > 0 && newHourlyPay > 0 && newNumDeps >= 0 && 
           newType >= 0 && newType <= 1 )
      {
        status = true;
        id = newId;
        name = newName;
        hourlyPay = newHourlyPay;
        numDeps = newNumDeps;
        type = newType;
      }

      return status;
    }  

     int Employee::getId()
    {
    return id;
    }

    string Employee::getName()
    { return name;
    }
    double Employee:: getHrpay()
    { return hourlyPay;
    }
     int  Employee::getDeps()
     {return numDeps;
     } 
     int Employee::getType()
     {
        return type;
     }



    int main()
    {   
        int newId;
        string newName;
        double newHourlyPay;
        int newNumDeps;
        int newType;
        double gross;
       // declare struct as an array and fill

        Pay pay[NUM_EMPS];
        Employee EmpInfo[6];

        Employee employee[NUM_EMPS];

        ofstream outputfile;
        ofstream hoursfile;

        //open file
        outputfile.open("employeeinfo.txt");
        if (!outputfile)
        {
        cout << "Error opening file!" << endl;
            }

        hoursfile.open("employeehours.txt");
        if (!hoursfile)
        {
        cout << "Error opening file!" << endl;
            }
        // get information from user


    for (int index =0; index < NUM_EMPS; index++)    
    {
        cout << "Please enter ID Number:  " << endl ;
        cin >>  newId;
        cin.ignore();
        cout << "Please enter Name: " << endl ;
        cin.width(20);
        getline (cin, newName);
        cout << "Please enter hourly pay:  " << endl ;
        cin >> newHourlyPay;
        cout<< "Please enter number of dependents:  " << endl ;
        cin >> newNumDeps;
        cout << "Please enter employee type:  " << endl ;
        cin >>newType;
        employee[index].set( newId,  newName,  newHourlyPay, newNumDeps, newType );
    }
    // write to file.

    for (int index = 0; index<NUM_EMPS; index++)
    {
    outputfile << employee[index].getId() << " " << employee[index].getName()<< setw(6) 
            << " " << setw(2) << employee[index].getHrpay() << setw(2) << employee[index].getDeps() <<
            setw(2) << employee[index].getType() << endl;
    }


    // Ask for hours.
    for (int index = 0; index < NUM_EMPS; index++)
        {cout << "Hours worked for " << employee[index].getName() << ": ";
    cin >> pay[index].Hours;
    }

    for (int index=0; index < NUM_EMPS; index++)
    {hoursfile <<  employee[index].getId() << setw(6) << pay[index].Hours << endl;
    }

    //close file 
    outputfile.close();
    hoursfile.close();

    return 0;

    }

It is often a good idea to start with a simple (simpler) version ...

Get that working and then upgrade from there.

This simpler version may get you started and give you some file read / write ideas to facilitate your progress in your coding problem ...

// dataWriteRead.cpp //

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



const int NUM_EMPS = 2; // keep small while testing //

const double TaxRate = 0.15;
const double DepCost = 20.00;
const double OverTimeRate = 1.5;
const double BasicHours = 37.5;

const char* EMP_FILE = "emp_file.txt";
const char* PAY_FILE = "pay_file.txt";

const char* MENU =
    "1 add employees\n"
    "2 enter hours worked\n"
    "3 get pays\n"
    "4 show all employees\n"
    "0 quit\n"
    "Enter your choice 0..4: ";


// some utilities useful here ... //
template< typename T >
T takeIn( const std::string& msg, const std::string& errmsg = "\nInvalid entry ... try again.\n" )
{
    T val;
    while( true )
    {
        std::cout << msg << std::flush;
        if( std::cin >> val && std::cin.get() == '\n' )
            break;
        else
        {
            std::cout << errmsg;
            std::cin.clear(); // clear cin error flags
            std::cin.sync(); // 'flush' cin stream ... //
        }
    }
    return val;
}

// return a (Non-empty) C++ string //
std::string takeInLine( const std::string& msg, bool okEmpty = false )
{
    std::string val;
    while( true )
    {
        std::cout << msg << std::flush;
        getline( std::cin, val );
        if( okEmpty )
            break;
        // else ...
        if( val.size() )
            break;
        // else ...
        std::cout << "\nEmpty line input is NOT valid here ...\n\n";
    }
    return val;
}
/*
template< typename T >
std::string numberToString( const T& val )
{
    std::istringstream iss;
    iss << val;
    return iss.str();
}
*/

char takeInChr( const std::string& msg )
{
    std::cout << msg << std::flush;
    std::string reply;
    getline( std::cin, reply );
    if( reply.size() )
        return reply[0];
    // else ...
    return 0;
}

bool more()
{
    char c =  takeInChr( "More (y/n) ? " ) ;
    if( c == 'n' || c == 'N' )
        return false;
    // else ...
    return true;
}

class Pay
{
    int id;
    double hours;
public:
    Pay( int id=0, double hours = 0.0 ) : id(id), hours(hours) {}

    void set_id( int newId ) { id = newId; }
    void set_hours( double weeksHrs ) { hours = weeksHrs; }

    int get_id() const { return id; }

    double weeksPay( double rate ) const
    {
        double ot = hours - BasicHours;
        double otPay = 0.0;
        double basePay = 0.0;
        if( ot > 0 )
        {
            otPay = ot*rate*OverTimeRate;
            basePay = BasicHours*rate;
        }
        else
            basePay = hours*rate;

        return basePay + otPay;
    }
} ;


class Employee 
{
    int id;
    std::string name;
    double rate;
public:
    Employee( int id = 0, const std::string& name = "", double rate = 0.0 )
    : id(id), name(name), rate(rate) {}

    void set_id( int nid ) { id = nid; }
    void set_name( const std::string& nname ) { name = nname; }
    void set_rate( double nRate ) { rate = nRate; }

    int get_id() const { return id; }
    std::string get_name() const { return name; }
    double get_rate() const { return rate; }

} ;


int initial( Employee emps[], int max_size )
{
    int i = 0 ;
    for( ; ;  ) // an example C/C++ 'forever' loop //
    {
        std::cout << "For " << (i+1) << " of " << max_size << " ... \n" ;

        emps[i].set_name( takeInLine( "Enter name: " ) ) ;

        emps[i].set_id( takeIn< int > ( "Enter id: ") ) ;

        emps[i].set_rate( takeIn< double > ( "Enter hourly rate: ") ) ;

        ++ i ;

        if( i == max_size )
        {
            std::cout << "You have reached " << max_size
                      << ", the 'max_size' permitted here ...\n" ;
            break;
        }

        if( !more() ) break ;
    }
    return i;
}

// returns number of employees loaded from file
// or -1 if 'file-open' error ... //
int loadEmployeesFromFile( Employee emps[], int max_size )
{
    std::ifstream fin( EMP_FILE ) ;
    if( fin )
    {
        int i = 0, id ;
        double rate ;
        std::string line, name ;
        while( i < max_size && getline( fin, line ) )
        {
            // parse line ...
            std::istringstream iss(line); // construct iss from line //
            iss >> id ;
            iss.get() ; // skip one space
            getline(iss, name, '#' ) ; // eat # and stop there
            name.erase( name.size()-1 ) ; // erase space at end
            iss >> rate ;
            iss.ignore( 256 ) ; // ensure rest of line ignored //

            emps[i] = Employee( id, name, rate ) ;

            ++i ;
        }
        fin.close() ;
        return i ;

    }
    // else ...
    return -1; // file open error flag value //
}

bool writeEmployeesToFile( Employee emps[], int size )
{
    std::ofstream fout( EMP_FILE ) ;
    if( fout )
    {
        for( int i = 0; i < size; ++ i )
        {
            fout << emps[i].get_id() << ' ' << emps[i].get_name() << " # "
                 << emps[i].get_rate() << '\n';
        }
        fout.close();
        return true;
    }
    return false;
}


int showMenuGetChoice()
{
    return takeIn< int > ( MENU );
}

void showAll( const Employee emps[], int size )
{
    std::cout << "\nAll the employees are: \n";
    for( int i = 0; i < size; ++ i )
    {
        std::cout << "id: " << emps[i].get_id() << ", name: "
                  << emps[i].get_name() << ", rate: "
                  << emps[i].get_rate() << '\n';
    }
    std::cout << '\n';
}



int main()
{   
    using namespace std;

    //Pay pays[NUM_EMPS];
    Employee employees[NUM_EMPS];

    int size = 0;

    if( (size = loadEmployeesFromFile( employees, NUM_EMPS )) == -1 )
    {
        size = initial( employees, NUM_EMPS ) ;
        if( writeEmployeesToFile( employees, size ) )
            cout << size << " employees were written to file "
                 << EMP_FILE << '\n';
        else
            cout << "There was a problem opening file " << EMP_FILE << '\n';
    }
    else
        cout << size << " employees were read form file " << EMP_FILE << '\n';


    if( size > 0 )
    {
        int choice = -1;
        while( choice != 0 )
        {
             choice = showMenuGetChoice();
            switch( choice )
            {
                case 0: break; // quit //

                case 1: cout << "Add new employees ... yet to be coded \n"; break;
                case 2: cout << "Enter hours worked ... yet to be coded \n"; break;
                case 3: cout << "Get pays ... yet to be coded \n"; break;
                case 4: showAll( employees, size ); break;

                default :
                    cout << "Choice " << choice << " is NOT implemented here ...\n";
            }
        }
    }
}

Edited 1 Year Ago by David W

This article has been dead for over six months. Start a new discussion instead.