0

So here is cliff notes of my assignment:
-Use Inheritence
-Person(lname, fname, address) inherited by employee(rate, hours)


What I would like to do:
-Find how long my arrays have to be(depends on file length)
-open file
-go to function retrieve lastname from file
-go to another function retrieve firstname
...etc
-close file
after all arrays have been file i want to send array to another function to be printed

Now is this possible? to open a file in main and read data in another function? Because if I close the file each time i grab something from it it will start reading from the first line again.
MAIN

#include <cstdlib>
#include <iostream>
#include "employee.h"
#include <string>
#include <fstream> 
#include <cmath>

using namespace std;

int main()
{
    string line;
    double count = 0;
    int intcount = 0; 
    ifstream myfile;
    myfile.open ("input.txt");
//Get number of line files is to calculate size of arrays   
   while (!myfile.eof())
   { 
     getline(myfile, line);
     count++;
   };
   
   intcount = int(ceil(count*.25));
   
//intializing arrays        
    string lastName[intcount];
    string firstName[intcount];
    string address[intcount];
    double rate[intcount];
    double hours[intcount];

//filling arrayas
for(int i = 0; i < intcount; i++)
{
//Make instance of objects
    employee employee1;
//Gets------------------------
                              
//Sets------------------------
    lastName[i] = employee1.setLastName();
    firstName[i] = employee1.setFirstName();
    address[i] = employee1.setAddress();
    
    rate[i] = employee1.setRate();
    hours[i] = employee1.setHours();
    
    
};
    //employee1.display();
    myfile.close();
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

.H

#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
#include <iostream>
#include <cstdlib>

using namespace std;

class person
{     
      public:
//CONSTRUCTOR--------------------------------
        person();                                     

//GETS---------------------------------------
        string getLastName(void);
        string getFirstName(void);
        string getAddress(void);
//SETS---------------------------------------    
        string setLastName(void);
        string setFirstName(void);
        string setAddress(void);
        void display(void);
      
      private:
            
            string firstName;
            string lastName;
            string address;  
              
              
};

class employee : public person
{
      
    public:
//CONSTRUCTOR--------------------------------                                        
        employee();                                                
//GETS---------------------------------------        
        double getRate(void);
        double getHours(void);
//SETS---------------------------------------
        double setRate(void);
        double setHours(void);
        void display(string, string, string, double, double); 
                     
    private:  
        string firstName;
        string lastName;
        string address;  
        double rate;
        double hours;
            
      
};

.CPP

#include <iostream>
#include <cstdlib>
#include "employee.h"
#include <fstream>

using namespace std;

// CONSTRUCTOR
employee::person()
{          
    firstName = "";
    lastName = "";
    address = "";                 
                  
};
employee::employee()
{              
   rate = 0.0;
   hours = 0.0;                          
                    
};



//FUNCTIONS---------------------------------------------------------------------

string getLastName(void)
{
   return lastName;            
};
        
string getFirstName(void)
{
       
   return firstname;    
};

string getAddress(void)
{
           
   return address;    
};      
 
string setLastName(void)
{
       getline(myfile, lastName);

       return lastName;

};

string setFirstName(void)
{
       getline(myfile, firstName);

       return firstName;
};     

string setAddress(void)
{
       getline(myfile, address);

       return address;

};

void display(void)
{



};

double getRate(void)
{
       
   return rate;    
};       

double getHours(void)
{
       
   return hours;   
};

double setRate(void)
{
       getline(myfile, rate);

       return rate;
     
};

double setHours(void)
{
      getline(myfile, hours);

      return hours;
     
};     
     
void display(string, string, string, double, double)
{
     
     
};
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by prvnkmr194
0

Create file pointer globally or pass the file as an argument to function.
You can also try move the file pointer you one this what you like suit you.
If you get problem in this mention that

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.