Can anyone figure out why I get -9.032654e something from my biWeeklySalary function?

#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include "HireDate.h"

#include <string>
using std::string;

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

    void setFirstName( const string & );
    string getFirstName() const; 

    void setLastName( const string & );
    string getLastName() const;

    void setSocialSecurityNumber( const string & );
    string getSocialSecurityNumber() const;

    void print() const;

private:
    string firstName;
    string lastName;
    string socialSecurityNumber;
    const HireDate hireDate;
};

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

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

//constructor
Employee::Employee()
{

}

//Constructor 
Employee::Employee(const string &first, const string &last, const string &ssn, const HireDate &dateOfHire )
    : firstName( first ), lastName( last ), socialSecurityNumber( ssn ), hireDate( dateOfHire )
{

}

Employee::~Employee()       //destructor
{

}

//function to set Employee's first name
void Employee::setFirstName( const string &first )
{
    firstName = first;
}

//function to get Employee's first name
string Employee::getFirstName() const
{
    return firstName;
}

//function to set Employee's last name
void Employee::setLastName( const string &last )
{
    lastName = last;
}

//function to get Employee's last name
string Employee::getLastName() const
{
    return lastName;
}

// function to set Employee's Social Security Number
void Employee::setSocialSecurityNumber( const string &ssn )
{
    socialSecurityNumber = ssn;
}

//function to get Employee's Social Security Number
string Employee::getSocialSecurityNumber() const
{
    return socialSecurityNumber;
}

//function to print Employee object
void Employee::print() const
{
    cout << "Employee: " << getFirstName() << ' ' << getLastName()
        << "\nSocial Security Number: " << getSocialSecurityNumber()
        << "\nTotal of your paycheck: " << MailCarrier::getTotalPay() << endl;
}
#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;

    double getTotalPay() const;

    void getHoursFromUser();

    double biWeeklySalary();

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 total pay for paycheck
double MailCarrier::getTotalPay() const
{
    return totalPay;
}

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

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

//Function to calculate total pay
double MailCarrier::biWeeklySalary()
{
    double pay = 0;
    double totalPay = 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;
}

> for (int i = 0; i <= 12; i++)
> for (int j = 0; j <= 11; j++)
> for (int k = 0; k < 11; k++)
Only one of those loops correctly accesses all the elements of your array.

One of them steps off the end, no doubt trashing something else into the bargain.

for ( i = 0 ; i < N ; i++ )
No need for <=
No need to subtract one from the size of the array.

I fixed the loop which stepped of the array and am still getting -9.25596e+061 for all calculation of biWeeklySalary

Can anyone figure out why I get -9.032654e something from my biWeeklySalary function?

#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include "HireDate.h"

#include <string>
using std::string;

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

    void setFirstName( const string & );
    string getFirstName() const; 

    void setLastName( const string & );
    string getLastName() const;

    void setSocialSecurityNumber( const string & );
    string getSocialSecurityNumber() const;

    void print() const;

private:
    string firstName;
    string lastName;
    string socialSecurityNumber;
    const HireDate hireDate;
};

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

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

//constructor
Employee::Employee()
{

}

//Constructor 
Employee::Employee(const string &first, const string &last, const string &ssn, const HireDate &dateOfHire )
    : firstName( first ), lastName( last ), socialSecurityNumber( ssn ), hireDate( dateOfHire )
{

}

Employee::~Employee()       //destructor
{

}

//function to set Employee's first name
void Employee::setFirstName( const string &first )
{
    firstName = first;
}

//function to get Employee's first name
string Employee::getFirstName() const
{
    return firstName;
}

//function to set Employee's last name
void Employee::setLastName( const string &last )
{
    lastName = last;
}

//function to get Employee's last name
string Employee::getLastName() const
{
    return lastName;
}

// function to set Employee's Social Security Number
void Employee::setSocialSecurityNumber( const string &ssn )
{
    socialSecurityNumber = ssn;
}

//function to get Employee's Social Security Number
string Employee::getSocialSecurityNumber() const
{
    return socialSecurityNumber;
}

//function to print Employee object
void Employee::print() const
{
    cout << "Employee: " << getFirstName() << ' ' << getLastName()
        << "\nSocial Security Number: " << getSocialSecurityNumber()
        << "\nTotal of your paycheck: " << MailCarrier::getTotalPay() << endl;
}
#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;

    double getTotalPay() const;

    void getHoursFromUser();

    double biWeeklySalary();

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++) 
// dont u think "i < 12" since array has 12 elements ie 0 - 12

         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 total pay for paycheck
double MailCarrier::getTotalPay() const
{
    return totalPay;
}

//Function to get hours from user
void MailCarrier::getHoursFromUser()
{
// dont u think "i < 12" since array has 12 elements ie 0 - 12
    for (int j = 0; j <= 11; j++)
    {
        cout << "Enter hours for" << Employee::getFirstName() << "day " << j+1 << ": ";
        cin >> hours[ j ];
    }
}//End function

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

    // dont u think "i < 12" since array has 12 elements ie 0 - 12
    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;
}

Correct me if i am wrong, but at different places you access and modify the array "hour" in different ways, please be consistent wiht your style and use the correct form of loop control variable to avoid subtle bugs. The would be problem areas have been marked by me in red.

If problem persists, repost.

Hope it helped, bye.

Those three loops have been fixed and I have instantiated two objects with different values for hours in the array yet, both calls to biWeeklySalary with two different objects show the same -9....e+061 value

> double totalPay;
This is in the class, as a private member variable

> double totalPay = 0;
This is in a function - it hides your class variable.

Which compiler are you using?

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