I am a newbie to C++ . i am trying to , i think, answer this question and i am lost many hours wasted. I think I've broken the problem done correctly.


1)i need to design a pay class that has data members for an employee’s hourly pay rate and number of hours worked.

2)Write a program with an array of seven pay objects.

3) a.The program should read the number of hours each employee worked b.and The program should read their hourly pay rate from a file

4) call class functions to store this information in the appropriate objects.

5) It should then call a class function, a. once for each object, b. to return the employees gross pay,

6) so this information can be displayed.

Issues: I can read data from the file ( it has to columns) but i cant read it into an object. so the object functions can store it to calculate the gross

// here what I have can anyone help me , i know this is probably simple but I'm complete confused and i have to understand arrays and classes before I move on.

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

using namespace std;

Class  Payroll

int hours;
double rate, gross;

Payroll()     // default constructor
hours = rate = 0;

// Set

 void setHours(int h)
{   hours = h;  }

void setRate(double r)

{   rate = r;   }

//  Get 
double getGross (int hours, double rate r])

gross =  hours * rate;
return gross;

// trying write a function that take array display results
void showfunction(int array[],int); // lost here too!

int main ()

Payroll sevenObjects[7];

const int EMP_RATE = 7 ;// sets number of Employees
const int EMP_HOURS = 7; 
int hours [EMP_HOURS];
double rate [EMP_RATE];
int count ;
ifstream datafile;  //  used to read  data

datafile.open ("payroll.dat");     // Open file
if( !datafile)
    cout << "error opening data file \n";

   /*  i tried  nested and getline, probably wrong. i tried  2D  Array[][] but didnt      know how to pass to member function */          

    for (count = 0 ; count < lines; count++)
        >> hours[count]>>rate[count];      
      cout << hours[count]<<"  " <<rate[count]<< endl;

  return 0;

40.0      10.00
38.5      9.50
16.0      7.50
22.5      9.50
40.0      8.00
38.0      8.00
40.0      9.00
3 Years
Discussion Span
Last Post by kal_crazy

I'm a bit confused regarding the actual question you're trying to ask, so, here's a solution to the problem of passing an array to a function:

// 1D representation
        void showFunction(int* arrayValues, int SIZE)
            for(unsigned i=0; (i < SIZE); i++)
                std::cout << arrayValues[i] << std::endl;

Here you have a 1D representation of how you would display the data in a 1D array, although, I would not do this in a class member function, as it's not really wise to physically have cout ops inside a class member function.

Here is a 2D representation:

// 2D representation
        void showFunction(int values[][5], int SIZE) 
            for(unsigned i=0; (i < 2); i++)
                for(unsigned j=0; (j < 5); j++)
                    std::cout << values[i][j] << std::endl;

Where 5 is the size of the array etc.. I don't really agree with 2D arrays set out like this, since, in memory, a 2D array is infact stored as a 1D block of memory, you just access it differently, so, for example, the "1D representation" example I gave in this example, could be used for 2D, just splitting or writing the clause where you know you are going to seperate the data is required.

Here is a messy implementation, or, example of the two functions uses:

// 1D
    int values[] = {1, 2, 3, 4, 5};
    const int SIZE = 5;

    // 2D
    int values2D[][5] = {
        {1, 2, 3, 4, 5},
        {1, 2, 3, 4, 5}

    Foo f;

    f.showFunction(values, SIZE);
    std::cout << std::endl;
    f.showFunction(values2D, SIZE);

I would stick to vectors, but, as your post says you are not allowed to use them. Or at least move towards the std::array<#,#> structure which I assume is accepted in most standards now.

Hope this helps a little.


In your default constructor(line 17) why did you make hours equal to rate ?

Since rate and hours are different shouldn't it be like this:

Payroll()     // default constructor
    hours = 0;
    rate = 0;
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.