-4

Please Note: I am advised to use arrays, functions, parameter passing to achieve my goal.

Another calculator-program student in need of help...How many of these have you all seen? In all seriousness, I need to use functions and parameter passing to create 4 employees, and then calculate the total gross pay, the total federal tax, the total state tax and the total net pay of all four of the employees. So far, I've been able to make a program that does this for only 1 employee. My question is in regards to: how do I expand this code to record 4 employee records and then tally the aforementioned totals? I was thinking of putting it inside a for-loop starting at i<4, but I'm not exactly sure. My biggest concern is using parameter passing because I need to use parameter passing. Here is the code:

#include<iostream>
#include<string>
#include<iomanip>

using namespace std;

string employeeName;
float overtime;
float grossPay;
float hoursWorked;
float hourlyRate;
float statetaxOwed;
float statetaxRate;
float fedtaxOwed;
float fedtaxRate;
float netPay;

int main()
{
    cout << "Please enter the Employee's Name: ";
    getline(cin, employeeName);
    cout << "Please enter your hours worked: ";
    cin >> hoursWorked;
    cout << "Please enter your hourly rate: ";
    cin >> hourlyRate;
    cout << "Please enter the Federal Tax Rate: ";
    cin >> fedtaxRate;
    cout << "Please enter the State Tax Rate: ";
    cin >> statetaxRate;

    if (hoursWorked>40){
        hoursWorked = ((hoursWorked-40) * (1.5)) + 40;
    }
    else {
        hoursWorked=hoursWorked;
    }
    grossPay = hoursWorked * hourlyRate;
    fedtaxOwed = grossPay * (fedtaxRate/100);
    statetaxOwed = grossPay * (statetaxRate/100);
    netPay = (grossPay-fedtaxOwed- statetaxOwed);

    cout << setprecision(2) << showpoint << fixed;
    cout << "\nThe employee's name is: " << employeeName << endl;
    cout << "The Gross Pay is: $" << grossPay << endl;
    cout << "The Federal Taxes Owed is: $" << fedtaxOwed << endl;
    cout << "The State Taxes Owed is: $" << statetaxOwed << endl;
    cout << "The Net Pay for the Employee is: $" << netPay << endl;
}

Ideally the flow would operate like this:

Input: Employee 1 Name, Hours Worked, Hourly Rate, Fed Tax Rate, and State Tax Rate

Input: Employee 2 Name, Hours Worked, Hourly Rate, Fed Tax Rate, and State Tax Rate

Input: Employee 3 Name, Hours Worked, Hourly Rate, Fed Tax Rate, and State Tax Rate

Input: Employee 4 Name, Hours Worked, Hourly Rate, Fed Tax Rate, and State Tax Rate

{C A L C U L A T E}

Print: Employee 1 Name, Gross Pay, Fed Taxes Owed, State Taxes Owed, Net Pay

Print: Employee 2 Name, Gross Pay, Fed Taxes Owed, State Taxes Owed, Net Pay

Print: Employee 3 Name, Gross Pay, Fed Taxes Owed, State Taxes Owed, Net Pay

Print: Employee 4 Name, Gross Pay, Fed Taxes Owed, State Taxes Owed, Net Pay

Print: Total Gross Pay, Total Federal Taxes Owed, Total State Taxes Owed, Total Net Pay [THIS IS ACROSS ALL FOUR EMPLOYEES]

Print: Calculated Overtime Pay Total and Print # of Employees that worked overtime

  • 3
    Learn to use arrays / containers and what the `struct` or `class` keyword does. – PaulMcKenzie Jan 31 '17 at 01:05
  • 1
    And functions. From the description, this assignment is big on functions. `main` probably won't satisfy the marker. – user4581301 Jan 31 '17 at 01:07
  • we're not using classes yet, though Iam familiar with classes from java...if main doesn't satisfy the marker, what would? – user7484153 Jan 31 '17 at 01:09
  • @user7484153 -- Please post the rules of what you can or cannot use, so that legitimate answers aren't rejected. – PaulMcKenzie Jan 31 '17 at 01:12
  • What about a simple `struct`? Can you use that? – Greg Kikola Jan 31 '17 at 01:12
  • So what about arrays? See, we need to keep asking. Also, structs and classes can be passed as parameters. – PaulMcKenzie Jan 31 '17 at 01:14
  • Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Jan 31 '17 at 01:14
  • Like I said, just functions and parameter passing are all I can use. everything else I cannot use. I don't know all of computer science to list them out for you, unfortunately, so you'll have to take my word for it. – user7484153 Jan 31 '17 at 01:16
  • Arrays have nothing to do with parameter passing. Can you use arrays or not? – PaulMcKenzie Jan 31 '17 at 01:16
  • @user7484153 You can pass structs as parameters, what's the problem actually? – πάντα ῥεῖ Jan 31 '17 at 01:17
  • Did you read my last comment? Maybe it didn't post. I have to use _only functions and parameter passing_ so that does _not_ include arrays, unfortunately. I realize there may be a very simple way to do this in one LOC, but I have restrictions I am working with. – user7484153 Jan 31 '17 at 01:18
  • *Banging head on wall* -- Arrays have nothing to do with parameter passing. It is a way to store the data you're reading in so that you can loop (just as you stated in your question). – PaulMcKenzie Jan 31 '17 at 01:19
  • A structure is a class that defaults to lower restrictions on data accesses. Don't see this as an attack on you, but I think you may be reading too much into the restrictions. Any assignment that basically translates into English as "Make an array of structures" and won't let you use arrays or structures is tragically flawed. – user4581301 Jan 31 '17 at 01:19
  • @user7484153 _Parameter passing_ covers a wide range of what you can do. – πάντα ῥεῖ Jan 31 '17 at 01:19
  • Might be a better idea to speak with your professor or a TA. Without knowing exactly what the problem is asking you to do, it's kind of hard to help. – Greg Kikola Jan 31 '17 at 01:19
  • I don't know what "passing a struct as a parameter" means. So I'll go have to look it up and try for myself, because honestly I'm just trying to write my code in a way that uses parameter passing, functions and accepts 4 employee datas to calculate. Currently it only accepts one employee data because I was trying to start off small since C++ is so unintuitive compared to Python,. – user7484153 Jan 31 '17 at 01:20
  • If that's what you mean by arrays, then yes we can use arrays. Absolutely. – user7484153 Jan 31 '17 at 01:21
  • Then you need to create an array of 4 items of each one of those entities. Note that Python also has arrays, as does nearly every single computer language has. Imagine if it were 1000 employees instead of 4. – PaulMcKenzie Jan 31 '17 at 01:22
  • @user7484153 Stop **YELLING** in your question please. Such isn't well achieved here. – πάντα ῥεῖ Jan 31 '17 at 01:23
  • In python I would just make a `def` for inputting name,hoursworked, hourly rate, fed tax rate, and state tax rate...then return those amounts. Then I'd have a `def` to calulate and then a `def` to display and then in my main I would organize it in a forloop, calling what I need appropriately. Outside of the `def` I would display the running totals. In C++ this becomes an absolute nightmare. – user7484153 Jan 31 '17 at 01:25
  • I amended it to lower case. Wasn't yelling. – user7484153 Jan 31 '17 at 01:27
  • In C++, it's easier, seriously. – PaulMcKenzie Jan 31 '17 at 01:31
  • You already have a background in programming so this might be helpful in getting the terminology differences sorted out: https://isocpp.org/images/uploads/2-Tour-Basics.pdf It is a massively truncated version of the C++ Programming Language by this dude named Bjarne Stroustrup. He's pretty good at explaining this stuff. – user4581301 Jan 31 '17 at 01:31
  • @user7484153 -- The term "array" is the defacto term to use for an aggregate of identical data items in the world of computer languages. The term "def" isn't well known outside of the wold of Python. A person who knows no C++, but knows, say Fortran, Java, Pascal, Basic, etc. knows what is meant by "an array of x". – PaulMcKenzie Jan 31 '17 at 01:36
  • _Hangs hipster Python head in shame_ – user7484153 Jan 31 '17 at 01:53

1 Answers1

0

First, use a struct to describe one employee:

struct EmployeeData
{
    string employeeName;
    float overtime;
    float grossPay;
    float hoursWorked;
    float hourlyRate;
    float statetaxOwed;
    float statetaxRate;
    float fedtaxOwed;
    float fedtaxRate;
    float netPay;
};

Then use an array or other container class to describe more than one EmployeeData:

EmployeeData employee[4];  // array of 4 employees

Then to access each employee, it would be:

employee[0] // first employee
employee[1] // second employee
employee[2] // third employee
employee[3] // fourth employee

Note the number in square brackets. You can now loop over the employee array:

// input the data for the 4 employees
for (int i = 0; i < 4; ++i)
{
   cin >> empployee[i].employeeName;
   cin >> empployee[i].overtime;
   // etc..
}

To define the function:

void calculate_stats(Employee& theEmployee)
{
    // theEmployee is the employee passed into the function
    //...
}

And then to call the function:

int main()
{ 
    //...
    for (int i = 0; i < 4; ++i)
    {
       calculate_stats(employee[i]);
    }
    //...
}

That is basically the skeleton. You can pick out the parts that you say you can use, if there are any.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45