0

I am taking a class on C++ currently and need help with a certain part of my code. It all compiles but when I go to test it I am only allowed to input one value into the window before the rest of the code advances through without input.

For reference, this is the question I am answering: 11. Payroll

Design a PayRoll class that has data members for an employee’s hourly pay rate, number of hours worked of type double . The default constructor will set the hours worked and pay rate to zero. The class must have a mutator function to set the pay rate for each employee and hours worked. The class should include accessors for both the hours worked and the rate of pay. The class should lastly have a getGross function that will return a double calculated by multiplying the hours worked by the rate of pay.

Write a program with an array of seven PayRoll objects . The program should ask the user for the rate of pay for each employee and the number of hours each employee has worked. Be sure to include an employee claiming to work more then 60 hours per week. Print out, the array number of the employee, the hours worked, the rate of pay, and the gross pay, of all the employee's each on their own line. Set the precision for printing the doubles to two decimal places.

Input Validation: Do not accept values greater than 60 for the number of hours worked, simply have the set function set number of hours worked to 60, the maximum allowed.

My code is as follows:

#include <iostream>
#include <iomanip>

//Garrett Bartholomay
/*This program defines and implements the Payroll class.
 * The class is then used in a program that calculates gross pay for an array of
 * Payroll objects after accepting values for hours and pay rate from standard input*/

class Payroll
{
private:
    int hoursWorked;
    double payRate;
public:
    Payroll();
    Payroll(int, double);
    void setHours(int);
    void setPayRate(double);
    int getHours() const;
    double getPayRate() const;
    double getGross()const;
};
Payroll::Payroll()
{
    hoursWorked = 0;
    payRate = 0.0;
}
Payroll::Payroll(int h, double r)
{
    payRate = r;
    hoursWorked = h;
}
void Payroll::setHours(int h)
{
    hoursWorked = h;
}
void Payroll::setPayRate(double p)
{
    payRate = p;
}
int Payroll::getHours() const
{
    return hoursWorked;
}
double Payroll::getPayRate() const
{
    return payRate;
}
double Payroll::getGross() const
{
    double gross = static_cast<double>(hoursWorked) * payRate;
    return gross;
}

using namespace std;
int main()
{

    const int NUM_EMPLOYEES = 7;
    Payroll employee[NUM_EMPLOYEES];
    int pay;
    int hours;
    int i;
    double grossPay;

    for (i = 0; i < NUM_EMPLOYEES; i++)
    {

        cout << "Enter the # " << (i) << " employee's rate of pay per hour: ";
        cin >> pay;
        cout << "Enter the # " << (i) << " employee's hours worked for the week: ";
        cin >> hours;

        employee[i].setPayRate(pay);
        employee[i].setHours(hours);        
    }
    cout << "\n\nHere is the gross pay for each employee:\n";
    cout << fixed << showpoint << setprecision(2);
    for (int i = 0; i < NUM_EMPLOYEES; i++)
    {
        grossPay = employee[i].getGross();
        cout << "The gross pay for employee # " << (i) << " is: " << grossPay << endl;      
    }
    return 0;
}

The input resides within the loops.

Any help would be greatly appreciated.

Thanks,

G

  • Welcome to SO! homework type of questions are usually discouraged. Having said that, please summarise your question in short rather than 2 blocks of big paragrahphs that usually cause boredom for the users here. – ha9u63a7 Nov 09 '14 at 18:30
  • We certainly know what the homework question is. Now, what is *your* question about your code? – WhozCraig Nov 09 '14 at 18:39
  • Your program works fine for me. Are you entering a non-number? – Galik Nov 09 '14 at 18:39
  • 1
    Ah its probably because you are asking for `int` (integer) and typing in *real* numbers. Try making `pay` and `hours` type `double` and maybe add some error cheking. – Galik Nov 09 '14 at 18:42
  • 1
    Check [How to test whether stringstream operator>> has parsed a bad type and skip it](http://stackoverflow.com/questions/24504582/how-to-test-whether-stringstream-operator-has-parsed-a-bad-type-and-skip-it) – πάντα ῥεῖ Nov 09 '14 at 19:14
  • hagubear- Thanks for the advice, I just thought this was appropriate because I was not asking for the answer to the problem, and had already made it that far by making the code from scratch. – Garrett Bartholomay Nov 10 '14 at 04:10
  • Galik- thank you! I made such a silly mistake by declaring pay as an integer. That seems to be the problem. Thanks again! – Garrett Bartholomay Nov 10 '14 at 04:10

0 Answers0