I have instantiated an object, bob, and have an array with hours for each day bob has worked. When I try to call bob.biWeeklySalary( hours[] ) it doesn't work. I'm not really sure how to use the array here. The compiler just says that hours is an undeclared identifier.

Your help is very appreciated

#include <iostream>
using std::cout;
using std::endl;


#include "MailCarrier.h"
#include "HireDate.h"
#include "Employee.h"

int main()
{
    HireDate bobHire( 4, 8, 2004 );

    MailCarrier bob( "Bob", "Johnson", "534-44-9551", bobHire, 19.52 );

    bob.getHoursFromUser();

    bob.biWeeklySalary(  );

    bob.Employee::print();
}
#ifndef MAILCARRIER_H
#define MAILCARRIER_H

#include "Employee.h"
#include "HireDate.h"

#include <iostream>
using std::cout;
using std::endl;

class MailCarrier : public Employee
{
public:
    MailCarrier();
    MailCarrier( const string &, const string &, const string &, const HireDate &, double );

    void setPayRate( double );
    double getPayRate() const;

    void getHoursFromUser();

    double biWeeklySalary( double );

private:
    double payRate;
    double hours[12];
    double totalPay;

};
#endif
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include "MailCarrier.h"

//Constructor
MailCarrier::MailCarrier()
{
    for (int i = 0; i <= 12; i++)
        hours[i] = 0;
}

//Constructor
MailCarrier::MailCarrier( const string &first, const string &last, const string &ssn,
                        const HireDate &, double pay )
    //explicitly call base-class constructor
    : Employee( first, last, ssn, HireDate( ) )
{
    setPayRate( pay ); // validate and store payRate
}

// function to set payRate
void MailCarrier::setPayRate( double pay )
{
    payRate = ( pay > 0.0 && pay < 35.0 ) ? pay : 17.17;
}

double MailCarrier::getPayRate() const
{
    return payRate;
}

//Function to get hours from user
void MailCarrier::getHoursFromUser()
{

    for (int j = 0; j <= 11; j++)
    {
        cout << "Enter hours for day " << j+1 << ": ";
        cin >> hours[ j ];
    }
}//End function

//Function to calculate total pay
double MailCarrier::biWeeklySalary( double hours )
{
    double pay = 0;

    for (int k = 0; k <= 11; k++)
    {
    if ( hours[ k ] > 10)
        pay = ((payRate * 2 * ( hours[ k ] - 10)) + (payRate * 1.5 * 2) + (payRate * 8));
    else if ( hours[ k ] > 8)
        pay = ((payRate * 1.5 * (*hours[ k ] - 8)) + ( payRate * 8));
    else if ( hours[ k ] >= 0)
        pay = payRate * 8;
    else cout << "Hours entered incorrectly, Please try again!\n" << endl;
    totalPay += pay;
    }
    
    return totalPay;
}

Recommended Answers

All 10 Replies

Didn't see your complete code but in general Prototype should be like this double biWeeklySalary( double *hours ) and calling of function should be like this bob.biWeeklySalary( hours )

and I had bob.biWeeklySalary( hours )
also tried bob.biWeeklySalary ( hours[] )
bob.biWeeklySalary( *hours )

None worked

double MailCarrier::biWeeklySalary( double hours[] )
{
    double pay = 0;

    for (int k = 0; k <= 11; k++)
    {
    if ( hours[ k ] > 10)
        pay = ((payRate * 2 * ( hours[ k ] - 10)) + (payRate * 1.5 * 2) + (payRate * 8));
    else if ( hours[ k ] > 8)
        pay = ((payRate * 1.5 * (*hours[ k ] - 8)) + ( payRate * 8));
    else if ( hours[ k ] >= 0)
        pay = payRate * 8;
    else cout << "Hours entered incorrectly, Please try again!\n" << endl;
    totalPay += pay;
    }
    
    return totalPay;
}

With array hours, you need to pass the size also. bob.biWeeklySalary( hours , size) and change the prototype accordingly.

I haven't had much use of C/C++, but I noticed that the getHoursFromUser function has been declared as void.
Void may work differently with functions in C/C++, but a void method in Java is one which does not return a value. Is that relevant?

I haven't had much use of C/C++, but I noticed that the getHoursFromUser function has been declared as void.
Void may work differently with functions in C/C++, but a void method in Java is one which does not return a value. Is that relevant?

It is not a problem. Since the objective of the mentioned function is to get the hours worked from user input, no return value is needed. So there is nothing wrong with declaring it as void.

I appreciate the help guys but I can't seem to figure it out. here is what I have changed.

// prototype
double biWeeklySalary( double [], int ); 

// definition
double MailCarrier::biWeeklySalary( double hours[], int size )

private:

double hours[ 12 ];

bob.biWeeklySalary( hours, 12 ) returns an error where hours is undeclared

I appreciate the help guys but I can't seem to figure it out. here is what I have changed.

// prototype
double biWeeklySalary( double [], int ); 

// definition
double MailCarrier::biWeeklySalary( double hours[], int size )

private:

double hours[ 12 ];

bob.biWeeklySalary( hours, 12 ) returns an error where hours is undeclared

Hi, I am a newbie so am not really sure but I have a suggestion.

1. Return an array from get getHoursFromUser()
2. Take a new array, say new_array and initialize it using the returned value from getHoursFromUser().
3. Pass that new_array in your function that calculates the salary.

If that is wrong can you comment on that please. Thank you .

commented: welcome +3

1. Return an array from get getHoursFromUser()
2. Take a new array, say new_array and initialize it using the returned value from getHoursFromUser().
3. Pass that new_array in your function that calculates the salary.

If that is wrong can you comment on that please. Thank you .

No need to return an array because the array is a member of the class. Anything you modify in it, will be permanent. It is not necessary to pass member data to a member function also. That is the advantage of object oriented programming. Member Functions can act on Member data without explicit passing as variables.

Hi, I am a newbie so am not really sure but I have a suggestion.

1. Return an array from get getHoursFromUser()
2. Take a new array, say new_array and initialize it using the returned value from getHoursFromUser().
3. Pass that new_array in your function that calculates the salary.

If that is wrong can you comment on that please. Thank you .

Well someone suggested that I don't need to pass any arguments to the function because the array already has the values that are needed from the getHoursFromUser function. So I changed the biWeeklySalary function to receive no arguments but, the algorithm or something is messed up somewhere because the output of the function with 80 hours total is 1 and it should come out to 1561.60

Hi all,
Arrays are passed-by-reference as function/method arguments means that there is no need to return them with as the functions return value and their value changes as you change them in your function/method or anywhere.Arrays are unlike others which are passed-by-value.

Good luck.

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.