1 Replies - 4549 Views - Last Post: 31 August 2012 - 09:40 AM Rate Topic: -----

#1 williamwil88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 28-April 08

Error Reading Text file into Object Array

Posted 31 August 2012 - 09:20 AM

I am attempting to read a text input file into an object array. I successfully read the first integer and string. Unfortunately the rest of the line drops off. In addition, the loop only reads the first line of the file. There are six lines of text. I am attempting to find a solution without modifying the class variables. Is there another method to read the file into the object array? What error am I making in my loop where I am unable to read subsequent lines on the next iteration of the loop. Any assistance you can provide is appreciated. Thanks

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

//Class declaration for Employee
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:
    Employee( int initId=0, string initName="", 
              double initHourlyPay=0.0, 
              int initNumDeps=0, int initType=0 );  // Constructor

    bool set(int newId, string newName, double newHourlyPay,
             int newNumDeps, int newType);
             
             //Prototypes
             int getEmpID();
             string getEmpName();
             double getHourlyPay();
             int getDependents();
             int getEmpType();
    
}; 
//End of Class Declaration

/*//////////////////////////////////////////////////////////
Employee class Constructor
////////////////////////////////////////////////////////////*/

Employee::Employee( int initId, string 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;    
  }
}

/*//////////////////////////////////////////////////////////
Employee class set function
////////////////////////////////////////////////////////////*/


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;
}


/*//////////////////////////////////////////////////////////
Employee class get functions
////////////////////////////////////////////////////////////*/

//* get employee ID function/*
int Employee::getEmpID()
{
    return id;
}
//* get employee Name function/*
string Employee::getEmpName()
{
    return name;
}
//* get employee HourlyPay function/*
double Employee::getHourlyPay()
{
    return hourlyPay;
}
//* get employee number of dependents function/*
int Employee::getDependents()
{
    return numDeps;
}
//* get employee Type function/*
int Employee::getEmpType()
{
    return type;
}

/*//////////////////////////////////////////////////////////
Struct Declaration for PayInfo
////////////////////////////////////////////////////////////*/

struct PayInfo
{    
     int empID;
     int hoursWorked;
     double normalPay;
    
     
};




int main(int argc, char *argv[])
{
    
    const int NUM_Employees = 7; // Number of employees
   
   //Define an array fo Employee objects
    Employee masterFile[NUM_Employees]; //Array of class Employee
   PayInfo paycheck[NUM_Employees]; //Array of structures Payinfo
   
   //Main Variables
   int index;
   double grossPay=0.0,insurance=0,tax=0.0, netPay=0.0, totalGrossPay=0.0, totalNetPay=0.0;
   int count=0; //counter
   
   //Temp placeholder variables 
    int tempId;             // employee ID
    string tempName;        // employee name
    double tempHourlyPay;   // pay per hour
    int tempNumDeps;        // number of dependents
    int tempType;           // employee type
    int hoursWorked;         // hours worked by each employee
   
    
    //Fill masterFile class Array from file
   
   
   
   
   
   
  for(index = 0;index < NUM_Employees;index++) 
   {     
       //define and open input file
       ifstream inputfile1("master8.txt");
       
        if (!inputfile1)
        {
                        cout<< "Unable to open data file\n";
                       
        }
        else
        {
       //Read employee masterfile into Temp placeholder variables and set class array
       //Employee class object will do automatic validation of employee input
          inputfile1 >> tempId >> tempName >> tempHourlyPay >> tempNumDeps >> tempType;
           
           masterFile[index].set(tempId, tempName,tempHourlyPay, tempNumDeps, tempType);
          cout << "in Loop 1" << endl;
          cout << tempId << "   "<<tempName <<"    "<< tempHourlyPay<<"    " << tempNumDeps<<"   "<<tempType<<"   "<<endl;
        }
       //Close input file
       inputfile1.close();     
   }
   
   
   
   //Fill PayInfo Struct Array from file
   for(index = 0;index < NUM_Employees;index++) 
   {   
   
           //define and open input file
           ifstream inputfile2("trans8.txt");
           
           if (!inputfile2)
           {
              cout << "Unable to open data file\n";
               
           }
           else
           {   
           //Read transaction file into struct array
           
               inputfile2 >> paycheck[index].empID>> paycheck[index].hoursWorked;
               paycheck[index].normalPay=0;
               paycheck[index].normalPay = (paycheck[index].hoursWorked * masterFile[index].getHourlyPay());
            cout << "in Loop 2" << endl;
           } 
            
           //Close input file
           inputfile2.close();
               
           
   }
   
   
     //define and open output file
         //fstream outputFile("payroll8.txt");
     cout << fixed << showpoint << setprecision(2);
     cout << "ID" <<setw(20)<< "Name"<< setw(25) << "Gross Pay" << setw(15) << "Tax" << setw(15)<< "Insurance" << setw(25)<< "Net Pay" << endl;  
     
  // Payroll processing
  //Read arrays
  //if employee ID!0
  for (index = 0;index < NUM_Employees;index++)
  {
      if (masterFile[index].getEmpID()> 0)
      {  
          //if hoursWorked  == 0
          if (paycheck[index].hoursWorked > 0)
          {
                 //Calculate Grosspay validate type and apply payrate
              if ((masterFile[index].getEmpType() == 0)&& (paycheck[index].hoursWorked > 40)) //Union workers
              {
                  grossPay = (paycheck[index].hoursWorked - 40)*1.5 * masterFile[index].getHourlyPay() + 40* masterFile[index].getHourlyPay();
                  cout << "in Loop 3" << endl;
              }
              else
              {
                  grossPay =  paycheck[index].hoursWorked *  masterFile[index].getHourlyPay(); //Straight time 
              }
              
            
                  
            //Calculate Insurance cost for employees who have greater than 0 dependents
            insurance = masterFile[index].getDependents() * 20;
                
            //Calculate Tax for all employees
            tax = grossPay * .15;
                
            //Calculate net pay
            netPay = (grossPay - tax - insurance);
            
            //Sum all Gross pay
            totalGrossPay += grossPay;
            
            //Sum all Net Pay    
            totalNetPay += netPay;  
                                        
          }
          else
          {
              //error message if hoursWorked>0
              cout <<  "This employee has less then 0.0 hours worked. This employee will not be included in the payroll report" << endl;
              
          }
          
          
         
      } 
     else
      {  
          //else error message if employee ID is 0
          cout << "Invalid employee ID; employee will not be included in the payroll report" << endl;
         
            
      }  
            
  }
        
            
        //count complete transactions
        
     
 
        //Print Payroll Report to file-One line for each transaction/ tabular row and column 
     
        cout << masterFile[index].getEmpID() << setw(25)<< masterFile[index].getEmpName() << setw(25)<< grossPay << setw(25)<< tax << setw(25)<< insurance << setw(25)<< netPay << endl;
       
        //provide total for gross pay and net pay for all records
        cout << "Total Gross Pay:" << totalGrossPay << "" << "Total Net Pay: " << totalNetPay <<endl;
           
        //Close output file
        //outputFile.close();
      
      
 
   
 
     
//print to screen the total number of transactions that were processed correctly to screen




    system("PAUSE");
    return EXIT_SUCCESS;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Error Reading Text file into Object Array

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6011
  • View blog
  • Posts: 20,672
  • Joined: 05-May 12

Re: Error Reading Text file into Object Array

Posted 31 August 2012 - 09:40 AM

It would help if you show us the actual results you are getting, as well as your expected results. Attaching copies of the input files would also help.

Based on your vague description, it sounds like the problem may be with line 171, but I can't really tell without more details. Anyway, I'm making the guess that your employee name has spaces in it. (ex. John Doe) The stream extraction operator stops reading the string after first whitespace, and then it fails to read the second string as the hourly pay.

This post has been edited by Skydiver: 31 August 2012 - 09:40 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1