asked 174k views
4 votes
Please complete exercise #6 from page 173-174 (see scanned handout) from chapter 3. Please upload a single .cpp file and the screenshot(s) under this link. Please remember that you need to provide Employee Name and Gross Salary via keyboard, but all the results/report must be printed in a correct format in an output file as well as displayed on the screen.

1 Answer

2 votes

I'd be happy to help you with exercise #6 from page 173-174 in Chapter 3. However, as a text-based AI, I cannot create or upload files directly. Instead, I can provide you with a C++ code example for the exercise, and you can use it to complete the task. You can then compile and run the code on your own system to achieve the desired results.

Here's a sample C++ code that takes the employee's name and gross salary via keyboard input, calculates the net salary, and outputs the results to both the screen and an output file:

```cpp

#include <iostream>

#include <fstream>

#include <string>

#include <iomanip>

using namespace std;

int main() {

// Declare variables

string employeeName;

double grossSalary;

// Prompt user for input

cout << "Enter Employee Name: ";

getline(cin, employeeName);

cout << "Enter Gross Salary: $";

cin >> grossSalary;

// Calculate net salary (assuming a flat 20% tax rate)

double taxRate = 0.20;

double netSalary = grossSalary * (1 - taxRate);

// Output to the screen

cout << "\\Employee Name: " << employeeName << endl;

cout << "Gross Salary: $" << fixed << setprecision(2) << grossSalary << endl;

cout << "Net Salary: $" << fixed << setprecision(2) << netSalary << endl;

// Output to an output file

ofstream outputFile("employee_salary_report.txt");

if (outputFile.is_open()) {

outputFile << "Employee Name: " << employeeName << endl;

outputFile << "Gross Salary: $" << fixed << setprecision(2) << grossSalary << endl;

outputFile << "Net Salary: $" << fixed << setprecision(2) << netSalary << endl;

outputFile.close();

cout << "\\Results have been saved to 'employee_salary_report.txt'." << endl;

} else {

cout << "Unable to open the output file." << endl;

}

return 0;

}

```

Please note that in this code, we assume a flat 20% tax rate for calculating the net salary. You can adjust the tax rate as needed for your specific requirements. Once you run this code, it will prompt you for the employee's name and gross salary, calculate the net salary, and save the results to an output file named "employee_salary_report.txt."

answered
User Leantraxxx
by
7.5k points