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')...
C++ question. I want to write all output to the file "output.txt", but it will write...
C++ question. I want to write all output to the file "output.txt", but it will write just first character. Can you fix it? #include #include #include #include #include using namespace std; using std::cin; using std::cout; using std::string; // remove dashes convert letters to upper case string normalize(const string &isbn) { string ch; for (char i : isbn) { if (i == '-') { continue; // if "-" then skip it }    if (isalpha(i)) { i = toupper(i); // Check...
C++ question. I want to write all output to the file "output.txt", but it will write...
C++ question. I want to write all output to the file "output.txt", but it will write just first character. Can you fix it? #include #include #include #include #include using namespace std; using std::cin; using std::cout; using std::string; // remove dashes convert letters to upper case string normalize(const string &isbn) { string ch; for (char i : isbn) { if (i == '-') { continue; // if "-" then skip it }    if (isalpha(i)) { i = toupper(i); // Check...
The following code has some syntax error. Please fixed the error. Besides, I want the output...
The following code has some syntax error. Please fixed the error. Besides, I want the output in ASCII characters. Please give me the corrected code along with the screenshot of the output. def cbc_dec(ys): int xs = [] int iv = ("0XAA", 16) #in decimal int key = ("0X08", 16) int x0 = chr(((163 * (int (ys[0], 16) - key)) % 256) ^ iv) xs.append(x0) for i in range (1, len(ys)): int xi = chr((( 163 * (int (ys[i], 16)...
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT