A few function questions

Discussion in 'C++' started by Brian, Dec 9, 2005.

  1. Brian

    Brian Guest

    I wrote this program to calculate a users gross pay based on marital
    status and the number of exemptions they have. I have a few questions
    about my functions, heres my code:

    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <stdlib.h>
    using namespace std;


    double federal_taxes(char marital_status, int hours_worked, double
    hourly_rate);
    double state_taxes(int hours_worked, double hourly_rate);
    double gross_pay(int hours_worked, double hourly_rate);
    double net_pay(double pay, double federal_tax, double tax);


    int main()
    {
    string first_name;
    string last_name;
    char marital_status;
    int hours_worked;
    double hourly_rate;



    cout << "Enter your first name: ";
    cin >> first_name;
    cout << "Enter your last name: ";
    cin >> last_name;
    cout << "Enter the amount of hours worked: ";
    cin >> hours_worked;
    cout << "Enter the hourly rate: ";
    cin >> hourly_rate;
    cout << "Enter your marital status (M) for married (S) for single: ";
    cin >> marital_status;


    double federal_tax = federal_taxes(marital_status, hours_worked,
    hourly_rate);
    double tax = state_taxes(hours_worked, hourly_rate);
    double pay = gross_pay(hours_worked, hourly_rate);
    double total_pay = net_pay(pay, federal_tax, tax);



    cout << system("cls");
    cout << "Payroll information for ";
    cout << last_name;
    cout << ", ";
    cout << first_name;
    cout << ":";
    cout << endl;


    //HOURS WORKED
    cout << "Hours worked:";
    cout << fixed;
    cout << showpoint;
    cout << setprecision(2);
    cout << setw(26);
    cout << hours_worked;
    cout << endl;

    //HOURLY RATE
    cout << "Hourly rate:";
    cout << fixed;
    cout << showpoint;
    cout << setprecision(2);
    cout << setw(27);
    cout << hourly_rate;
    cout << endl;

    //MARITAL STATUS
    cout << "Marital status:";
    cout << setw(24);
    cout << marital_status;
    cout << endl;

    //NUMBER OF EXEMPTIONS
    cout << "Number of exemptions:";
    cout << endl;

    //GROSS PAY
    cout << "Gross pay:";
    cout << fixed;
    cout << showpoint;
    cout << setprecision(2);
    cout << setw(29);
    cout << pay;
    cout << endl;

    //FEDERAL TAX
    cout << "Federal tax:";
    cout << fixed;
    cout << showpoint;
    cout << setprecision(2);
    cout << setw(27);
    cout << federal_tax;
    cout << endl;

    //STATE TAX
    cout << "State tax:";
    cout << fixed;
    cout << showpoint;
    cout << setprecision(2);
    cout << setw(29);
    cout << tax;
    cout << endl;

    //NET PAY
    cout << "Net pay:";
    cout << fixed;
    cout << showpoint;
    cout << setprecision(2);
    cout << setw(31);
    cout << total_pay;
    cout << endl;

    }

    double federal_taxes(char marital_status, int hours_worked, double
    hourly_rate)
    {
    int num_exemptions;
    double federal_tax;


    switch(marital_status)
    {
    case 'M':
    cout << "Enter the number of exemptions: ";
    cin >> num_exemptions;

    if (num_exemptions == 0)
    federal_tax = (hours_worked * hourly_rate) * .15;

    else if (num_exemptions == 1)
    federal_tax = (hours_worked * hourly_rate) * .14;

    else if (num_exemptions == 2)
    federal_tax = (hours_worked * hourly_rate) * .13;

    else if (num_exemptions == 3)
    federal_tax = (hours_worked * hourly_rate) * .12;

    else if (num_exemptions >= 4)
    federal_tax = (hours_worked * hourly_rate) * .11;
    break;

    case 'S':
    cout << "Enter the number of exemptions: ";
    cin >> num_exemptions;

    if (num_exemptions == 0)
    federal_tax = (hours_worked * hourly_rate) * .17;

    else if (num_exemptions == 1)
    federal_tax = (hours_worked * hourly_rate) * .16;

    else if (num_exemptions == 2)
    federal_tax = (hours_worked * hourly_rate) * .15;

    else if (num_exemptions == 3)
    federal_tax = (hours_worked * hourly_rate) *.14;

    else if (num_exemptions >= 4)
    federal_tax = (hours_worked * hourly_rate) *.13;
    break;

    }


    return (federal_tax);
    }

    double state_taxes(int hours_worked, double hourly_rate)
    {
    double tax;
    tax = (hours_worked * hourly_rate) * .08;

    return tax;
    }

    double gross_pay(int hours_worked, double hourly_rate)
    {
    double pay;
    pay = hours_worked * hourly_rate;

    return pay;
    }

    double net_pay(double pay, double federal_tax, double tax)
    {
    double total_pay;
    total_pay = (pay - federal_tax) - tax;

    return total_pay;
    }

    First of all, can I get 2 values sent back to main from my
    federal_taxes function? I would like to get the value of exemptions
    back to main. How would I do it if its possible? Second, I think I want
    to create another function for the output. Is there a way to print all
    the output in the main function from an output_function?
    Brian, Dec 9, 2005
    #1
    1. Advertising

  2. Brian

    Guest

    to return many variables, you cad:

    1/ Put theses variables in arguments:
    int foo(int value1, int value2, int& return1, int& return2) {}

    the "int&" make your variables "writables"...

    2/ Return a vector of variables...
    , Dec 9, 2005
    #2
    1. Advertising

  3. Brian

    Andrew Ward Guest

    Brian wrote:
    > First of all, can I get 2 values sent back to main from my
    > federal_taxes function? I would like to get the value of exemptions
    > back to main. How would I do it if its possible?
    >



    You could introduce a composite type:

    class tax
    {
    public:
    double amount;
    int num_exceptions;
    tax(double a, int n) : amount(a), num_exceptions(n) {}
    };

    Then just return this from your function:

    return tax(federal_tax, num_exceptions);
    Andrew Ward, Dec 9, 2005
    #3
  4. Brian

    Marcus Kwok Guest

    Brian <> wrote:
    > I wrote this program to calculate a users gross pay based on marital
    > status and the number of exemptions they have. I have a few questions
    > about my functions, heres my code:
    >

    [code redacted]
    >
    > First of all, can I get 2 values sent back to main from my
    > federal_taxes function? I would like to get the value of exemptions
    > back to main. How would I do it if its possible?


    Others have already answered this so I will defer.

    > Second, I think I want
    > to create another function for the output. Is there a way to print all
    > the output in the main function from an output_function?


    Sure, you could create like

    void output_function(const string& first_name,
    const string& last_name
    /* and the other variables you need */);

    in which you have all your cout statements. However, it looks like you
    would need to pass in many variables. You could create a struct
    containing all the values you need, then just pass the struct to the
    output_function. Even better, you could create a class encapsulating
    all of the data you want (I'll just call it "Data" to illustrate, but
    you will probably want to pick a more meaningful name), and
    then provide

    ostream& operator<<(ostream& o, const Data& d)
    {
    cout << d.last_name << ", " << d.first_name << '\n';
    /* ... */

    return o;
    }

    Then, you can do like

    Data my_data;
    // ...
    cout << my_data;


    By the way, you know you can chain these "<<" statements together?

    > //HOURS WORKED
    > cout << "Hours worked:";
    > cout << fixed;
    > cout << showpoint;
    > cout << setprecision(2);
    > cout << setw(26);
    > cout << hours_worked;
    > cout << endl;


    can be written as

    cout << "Hours worked:"
    << fixed << showpoint << setprecision(2) << setw(26)
    << hours_worked
    << endl;

    --
    Marcus Kwok
    Marcus Kwok, Dec 9, 2005
    #4
  5. Brian

    Brian Guest

    wrote:
    > to return many variables, you cad:
    >
    > 1/ Put theses variables in arguments:
    > int foo(int value1, int value2, int& return1, int& return2) {}
    >
    > the "int&" make your variables "writables"...
    >
    > 2/ Return a vector of variables...


    You say the & make your variables "writeables". I thought they made
    them refrence points or a pointer?
    Brian, Dec 9, 2005
    #5
  6. Brian

    Default User Guest

    Brian wrote:

    > I wrote this program to calculate a users gross pay based on marital
    > status and the number of exemptions they have. I have a few questions
    > about my functions, heres my code:
    >


    > double federal_taxes(char marital_status, int hours_worked, double
    > hourly_rate)
    > {
    > int num_exemptions;
    > double federal_tax;
    >
    >
    > switch(marital_status)
    > {
    > case 'M':
    > cout << "Enter the number of exemptions: ";
    > cin >> num_exemptions;


    The question asking for exemptions should be back in main() along with
    marital status, then both values passed in. It certainly shouldn't be
    repeated in the separate switch cases.

    > if (num_exemptions == 0)
    > federal_tax = (hours_worked * hourly_rate) * .15;


    You should go with a better algorithm, and calculate the tax rate and
    pass that in instead of the status.

    double CalculateFederalRate(int exemptions, char status)
    {
    double rate = .11;

    if (exemptions < 4)
    {
    rate += (4 - exemptions) * .01);
    }

    // status value should be checked before this point!
    if (status != 'M')
    {
    rate += .02;
    }

    return rate;
    }

    This isolates the taxrate calculations so that if it changes, it's
    contained in one spot, easy to update. Then you remove the whole
    switch() from this function, which you've changed to:

    double federal_taxes(double federalRate, int hoursWorked, double
    hourlyRate)
    {
    double federalTax;

    federalTax = (hoursWorked * hourlyRate) * federalRate;

    return federalTax;
    }

    > First of all, can I get 2 values sent back to main from my
    > federal_taxes function? I would like to get the value of exemptions
    > back to main. How would I do it if its possible?


    Either return a class or struct, or use reference parameters.

    > Second, I think I
    > want to create another function for the output. Is there a way to
    > print all the output in the main function from an output_function?


    You have to pass it all over. Your solution really should be using some
    sort of OO approach. The basic data should be member variables, with
    service functions to do the calculations, and a print one.

    You've written essentially a C program with iostream for IO. It should
    be rewritten as a C++ program.


    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
    Default User, Dec 9, 2005
    #6
  7. Brian

    Marcus Kwok Guest

    Brian <> wrote:
    > wrote:
    >> to return many variables, you cad:
    >>
    >> 1/ Put theses variables in arguments:
    >> int foo(int value1, int value2, int& return1, int& return2) {}
    >>
    >> the "int&" make your variables "writables"...

    >
    > You say the & make your variables "writeables". I thought they made
    > them refrence points or a pointer?


    The '&' does indeed make them references. This means that any changes
    you make to those variables in the function, also change the original
    variable that you passed in, so in that sense it makes them writable.

    The parameters without the '&' are passed by value, which means that the
    function gets its own local copy of the variables, and the originals are
    not changed.


    #include <iostream>

    void value(int x)
    {
    x = 5;
    }

    void reference(int& x)
    {
    x = 5;
    }

    int main()
    {
    int x = 42;
    std::cout << "x = " << x << '\n';

    std::cout << "calling value()\n";
    value(x);
    std::cout << "x = " << x << '\n';

    std::cout << "calling reference()\n";
    reference(x);
    std::cout << "x = " << x << '\n';

    return 0;
    }


    --
    Marcus Kwok
    Marcus Kwok, Dec 9, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Parker
    Replies:
    3
    Views:
    549
    Hans Kesting
    Jun 25, 2004
  2. =?Utf-8?B?TmV3Ymll?=
    Replies:
    1
    Views:
    412
  3. Replies:
    10
    Views:
    536
  4. Murali
    Replies:
    2
    Views:
    542
    Jerry Coffin
    Mar 9, 2006
  5. Sven Johansson
    Replies:
    9
    Views:
    113
    Ryan Davis
    Jan 2, 2006
Loading...

Share This Page