473,395 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

A few function questions

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?

Dec 9 '05 #1
6 1541
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 '05 #2
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);
Dec 9 '05 #3
Brian <br************@gmail.com> 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
Dec 9 '05 #4
ro************@gmail.com 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?

Dec 9 '05 #5
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.
Dec 9 '05 #6
Brian <br************@gmail.com> wrote:
ro************@gmail.com 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
Dec 9 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: CoolPint | last post by:
After upgrading to gcc 3.4.2 from gcc 3.2.3, I got compiler errors that I could not figure out. After reading other postings, I learned that my coding was not compliant to the standard in the first...
4
by: Erik Paul | last post by:
I got some maybe stupid questions about file output and functions. At first I have to say I'm programming on Linux. The first thing is I couldn't find a function which can tell me microseconds...
19
by: golson | last post by:
I have written a C function to copy a flash write routine that is in Flash memory to RAM memory. I have then created a function that uses a Function Pointer to run the copied function in RAM. ...
1
by: Klaus Vestergaard Kragelund | last post by:
Hi there I cannot seem to get this to work. In LabWindows I want to be able to call the Sleep function. But when I #Include "Windows.h" I get errors that it cannot build: "Redeclearation of...
89
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
3
by: franco ziade | last post by:
I am stuck :( I am trying to pass the address of a function as an argument to a function (some OS call to launch a task): Example: -------- Declaration of the OS "lunch a Task" LunchTask(...,...
64
by: Morgan Cheng | last post by:
Hi All, I was taught that argument valuse is not supposed to be changed in function body. Say, below code is not good. void foo1(int x) { x ++; printf("x+1 = %d\n", x); } It should be...
7
by: changs | last post by:
Hi, all I have a asm code, I suspect it sort of socket programming. Can anyone here give some instructions on how to determine the function or give the psudo-code in C? Thanks in advance! ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.