B
Brian
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?
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?