Question

In: Computer Science

C++ i want .00 at the output cout << fixed << setprecision (2);where should i put...

C++ i want .00 at the output

cout << fixed << setprecision (2);where should i put this line?

quesstion

13. Array of Payroll Objects
Design a PayRoll class that has data members for an employee’s hourly pay rate and
number of hours worked. Write a program with an array of seven PayRoll objects. The
program should read the number of hours each employee worked and their hourly pay
rate from a file and call class functions to store this information in the appropriate
objects. It should then call a class function, once for each object, to return the employee’s
gross pay, so this information can be displayed. Sample data to test this program can be
found in the payroll.dat file.

To create the payroll.dat file, please open notepad, copy/paste the following data and save the file as payroll.dat file. Please make sure to save the file in a place where your program file is located. In otherwords, please do not hard code the path for the payroll.dat .

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

Mimir Requirements:

Mimir file name must be:  CS939Homework8A.cpp

Here is the sample output.

Employee 1: 400.00

Employee 2: 365.75

Employee 3: 120.00

Employee 4: 213.75

Employee 5: 320.00

Employee 6: 304.00

Employee 7: 360.00

My Code

#include <iostream>
#include <fstream>

using namespace std;

class PayRoll {
private:
    double hourlyPayRate;
    double hoursWorked;
public:
    PayRoll(double hP, double hW) {
        hourlyPayRate = hP;
        hoursWorked = hW;
    }
    double getGrossPay() {
        return hourlyPayRate * hoursWorked;
    }
};

int main() {
    PayRoll** payrolls = new PayRoll * [7];
    fstream myfile("payroll.dat", std::ios_base::in);

    double rate, hours;
    int count = 0;

    while (myfile >> rate >> hours) {
        payrolls[count++] = new PayRoll(rate, hours);
    }

    for (int i = 0; i < 7; i++) {
        cout << "Employee " << (i + 1) << ": " << payrolls[i]->getGrossPay() << endl;
    }
}

Solutions

Expert Solution

To get precision of upto 2 decimal places, we have to use 'setprecision(2)' along with 'fixed'. To get employee grosspay upto 2 decimal places, the 3rd last line has to be edited to cout << "Employee " << (i + 1) << ": " << fixed <<setprecision(2) <<payrolls[i]->getGrossPay() << endl;

Here is updated code:

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

using namespace std;

class PayRoll {
private:
double hourlyPayRate;
double hoursWorked;
public:
PayRoll(double hP, double hW) {
hourlyPayRate = hP;
hoursWorked = hW;
}
double getGrossPay() {
return hourlyPayRate * hoursWorked;
}
};

int main() {
PayRoll** payrolls = new PayRoll * [7];
fstream myfile("payroll.dat", std::ios_base::in);

double rate, hours;
int count = 0;

while (myfile >> rate >> hours) {
payrolls[count++] = new PayRoll(rate, hours);
}

for (int i = 0; i < 7; i++) {
cout << "Employee " << (i + 1) << ": " << fixed <<setprecision(2) <<payrolls[i]->getGrossPay() << endl;
}
}

Code Photo:

Payroll.dat in same directory as CS939Homework8A.cpp file

Here is updated output:


Related Solutions

My output is incorrect when i put an empty string first_inside_quotes(' "" '). Not sure where...
My output is incorrect when i put an empty string first_inside_quotes(' "" '). Not sure where the issue lies import introcs def first_inside_quotes(s): ''' Returns the first substring of s between two (double) quote characters    Note that the double quotes must be part of the string. So "Hello World" is a precondition violation, since there are no double quotes inside the string.    Example: first_inside_quotes('A "B C" D') returns 'B C' Example: first_inside_quotes('A "B C" D "E F" G')...
Every C++ program starts with this function:____________________ 2. If A is > 100 I want cnt...
Every C++ program starts with this function:____________________ 2. If A is > 100 I want cnt to be equal to 32 but if A 100 or less I want cnt to be 2. Write a single line using a conditional operator to do this. 3. I want to store into B the remainder of dividing B by the total of A plus D. Write the statement: as a combined operator statement not using a combined operator 4. I want to...
2) Given the following C = 25 + 0.8Yd I* = 100 and assuming fixed prices...
2) Given the following C = 25 + 0.8Yd I* = 100 and assuming fixed prices a) What is the equilibrium level of NP ? b) What is the equilibrium level of savings ? c) What is the value of the multiplier ? d) If planned investment falls 10, ceteris paribus, what is the new level of equilibrium NP ? e) If the MPC increases to 0.85 what would equilibrium NP be with I*= 100 ? f) If the MPS...
I have n books with n different titles. I want to put them on the shelves...
I have n books with n different titles. I want to put them on the shelves in my library. How many different ways are there to arrange them on one shelf? On two shelves? On k shelves?
Consider a firm’s cost function c(y) = 4y^2 + 80, where the $80 is a quasi-fixed...
Consider a firm’s cost function c(y) = 4y^2 + 80, where the $80 is a quasi-fixed cost in the long run but a fixed cost in the short run. The marginal cost associated with this cost function is MC = 8y. Assume this firm operates in perfectly competitive markets. (a) If the price of the firm’s output is $48, how many units will this firm choose to produce? What will be this firm’s profit? (b) If the price of the...
The data table is too large to be put in the question here is where I...
The data table is too large to be put in the question here is where I collected the data: Visit the NASDAQ historical prices weblink(https://finance.yahoo.com/quote/GOOG/history/). First, set the date range to be for 1.16.18- 2.15.19. [Do this by clicking on the blue dates after “Time Period”. Next, click the “Apply” button. Next, click the link on the right side of the page that says “Download Data” to save the file to your computer]. Normal distribution Average=1110.68525 Standard Deviation= 65.5518337 Mode=...
If you are a manager in a highly competitive business such where should you put your...
If you are a manager in a highly competitive business such where should you put your most effort to maximize profit? Pricing or cost cutting?
c# code working but output not right, I need to output all numbers like : Prime...
c# code working but output not right, I need to output all numbers like : Prime factors of 4 are: 2 x 2 here is just 2 Prime factors of 7 are: 7 Prime factors of 30 are: 2 x 3 x 5 Prime factors of 40 are: 2 x 2 x 2 x 5 here is just 2,5 Prime factors of 50 are: 2 x 5 x 5 here is just 2,5 1) How I can fix it 2)I...
1. If y=C+I where C= 50+.75 Y and I = 50 What is Y, C, and...
1. If y=C+I where C= 50+.75 Y and I = 50 What is Y, C, and the multiplier? What if instead I = 50+.05Y? 2. If Y=C+I+G where C=C - aTo + Y(a - at) and I= lo + by solve for Y, C, I, and the multiplier. Given a = MPC= .75, MPT = .2, MPI = .15, C = 45, To = 40 Io = 60 and G=90 3. What is the present value of $1,000 paid at...
2. Consider the simplified national income model:     Y = C + I…………(1)                            Where Y...
2. Consider the simplified national income model:     Y = C + I…………(1)                            Where Y is national income, C is consumption, and I is investment. Consumption is determined by a behavioral equation, which in this problem takes the form      C= 3000+ .75 Y……..(2) Where Y and C are endogenous variables and Investment is exogenous, and, initially we assume I =1000……………….(3) (2-a) Determine the equilibrium level of national income (Y) and consumption (C) by using the matrix (linear) algebra...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT