B
brian.digipimp
I turned this in for my programming fundamentals class for our second
exam. I am a c++ newb, this is my first class I've taken. I got a good
grade on this project I'm just wondering if there is a better and more
efficient way to write this program.
//This program calculates the users Gross Pay and subtracts the tax
based off of Marital Status
//and the number of exemptions they select
#include <iostream>
#include <iomanip>
#include <string>
#include <stdlib.h>
using namespace std;
int main()
{
string FirstName; //Holds the user inputed First Name
string LastName; //Holds the user inputed Last Name
char MaritalStatus; //Holds the user inputed value for Marital
Status
double HoursWorked; //Holds the user inputed value of Hours Worked
double HourlyRate; //Hodls the user inputed value of Hourly Rate
double GrossPay; //Calculates and holds the users inputed value for
Gross Pay
double FederalTax; //Holds the calculations for calculating
FederalTax
double StateTax; //Holds the calculations for calculating StateTax
int NumExemptions; //Holds the user inputed value for Number of
Exemptions
cout << "Enter your first name:";
cin >> FirstName;
cout << "Enter your last name:";
cin >> LastName;
cout << "Input the amount of hours worked:";
cin >> HoursWorked;
cout << "Input the hourly rate:";
cin >> HourlyRate;
cout << "Input the marital status ('M' for married and 'S' for
single): ";
cin >> MaritalStatus;
GrossPay = HoursWorked * HourlyRate;
StateTax = GrossPay * .0825;
switch (MaritalStatus)
{
case 'S':
cout << "Input the number of exemptions:";
cin >> NumExemptions;
cout << system("cls");
cout << "Payroll information for ";
cout << LastName << ", ";
cout << FirstName << ":";
cout << endl;
cout << endl;
cout << "Hours Worked: ";
cout << setw(24);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << HoursWorked;
cout << endl;
cout << "Hourly Rate: ";
cout << setw(25);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << HourlyRate;
cout << endl;
cout << "Marital Status: ";
cout << setw(22);
cout << "Single";
cout << endl;
cout << "Number of Exemptions: ";
cout << setw(16);
cout << NumExemptions;
cout << endl;
cout << endl;
cout << "Gross Pay";
cout << setw(29);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << GrossPay;
cout << endl;
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << "Federal Taxes";
if (NumExemptions == 0)
{
cout << setw(25);
cout << (GrossPay * .179);
FederalTax = GrossPay * .179;
}
else if (NumExemptions == 1)
{
cout << setw(25);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << (GrossPay * .168);
FederalTax = GrossPay * .168;
}
else if (NumExemptions == 2)
{
cout << setw(25);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << (GrossPay * .157);
FederalTax = GrossPay * .157;
}
else if (NumExemptions == 3)
{
cout << setw(25);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << (GrossPay * .146);
FederalTax = GrossPay * .146;
}
else if (NumExemptions >= 4)
{
cout << setw(25);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << (GrossPay * .125);
FederalTax = GrossPay * .125;
}
cout << endl;
cout << "State Taxes";
cout << setw(27);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << GrossPay * .0825;
cout << endl;
cout << endl;
cout << "Net Pay";
cout << setw(31);
cout << (GrossPay - FederalTax - StateTax);
cout << endl;
break;
case 'M':
cout << "Input the number of exemptions:";
cin >> NumExemptions;
cout << system("cls");
cout << "Payroll information for ";
cout << LastName << ", ";
cout << FirstName << ":";
cout << endl;
cout << endl;
cout << "Hours Worked: ";
cout << setw(24);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << HoursWorked;
cout << endl;
cout << "Hourly Rate: ";
cout << setw(25);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << HourlyRate;
cout << endl;
cout << "Marital Status: ";
cout << setw(22);
cout << "Married";
cout << endl;
cout << "Number of Exemptions: ";
cout << setw(16);
cout << NumExemptions;
cout << endl;
cout << endl;
cout << "Gross Pay";
cout << setw(29);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << GrossPay;
cout << endl;
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << "Federal Taxes";
if (NumExemptions == 0)
{
cout << setw(25);
cout << (GrossPay * .151);
FederalTax = GrossPay * .151;
}
else if (NumExemptions == 1)
{
cout << setw(25);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << (GrossPay * .142);
FederalTax = GrossPay * .142;
}
else if (NumExemptions == 2)
{
cout << setw(25);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << (GrossPay * .133);
FederalTax = GrossPay * .133;
}
else if (NumExemptions == 3)
{
cout << setw(25);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << (GrossPay * .124);
FederalTax = GrossPay * .124;
}
else if (NumExemptions >= 4)
{
cout << setw(25);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << (GrossPay * .105);
FederalTax = GrossPay * .105;
}
cout << endl;
cout << "State Taxes";
cout << setw(27);
cout << GrossPay * .0825;
cout << endl;
cout << endl;
cout << "Net Pay";
cout << setw(31);
cout << GrossPay - FederalTax - StateTax;
cout << endl;
break;
default: cout << "Invalid Status, Please type either 'M' for married
or 'S' for single";
}
return 0;
}
exam. I am a c++ newb, this is my first class I've taken. I got a good
grade on this project I'm just wondering if there is a better and more
efficient way to write this program.
//This program calculates the users Gross Pay and subtracts the tax
based off of Marital Status
//and the number of exemptions they select
#include <iostream>
#include <iomanip>
#include <string>
#include <stdlib.h>
using namespace std;
int main()
{
string FirstName; //Holds the user inputed First Name
string LastName; //Holds the user inputed Last Name
char MaritalStatus; //Holds the user inputed value for Marital
Status
double HoursWorked; //Holds the user inputed value of Hours Worked
double HourlyRate; //Hodls the user inputed value of Hourly Rate
double GrossPay; //Calculates and holds the users inputed value for
Gross Pay
double FederalTax; //Holds the calculations for calculating
FederalTax
double StateTax; //Holds the calculations for calculating StateTax
int NumExemptions; //Holds the user inputed value for Number of
Exemptions
cout << "Enter your first name:";
cin >> FirstName;
cout << "Enter your last name:";
cin >> LastName;
cout << "Input the amount of hours worked:";
cin >> HoursWorked;
cout << "Input the hourly rate:";
cin >> HourlyRate;
cout << "Input the marital status ('M' for married and 'S' for
single): ";
cin >> MaritalStatus;
GrossPay = HoursWorked * HourlyRate;
StateTax = GrossPay * .0825;
switch (MaritalStatus)
{
case 'S':
cout << "Input the number of exemptions:";
cin >> NumExemptions;
cout << system("cls");
cout << "Payroll information for ";
cout << LastName << ", ";
cout << FirstName << ":";
cout << endl;
cout << endl;
cout << "Hours Worked: ";
cout << setw(24);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << HoursWorked;
cout << endl;
cout << "Hourly Rate: ";
cout << setw(25);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << HourlyRate;
cout << endl;
cout << "Marital Status: ";
cout << setw(22);
cout << "Single";
cout << endl;
cout << "Number of Exemptions: ";
cout << setw(16);
cout << NumExemptions;
cout << endl;
cout << endl;
cout << "Gross Pay";
cout << setw(29);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << GrossPay;
cout << endl;
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << "Federal Taxes";
if (NumExemptions == 0)
{
cout << setw(25);
cout << (GrossPay * .179);
FederalTax = GrossPay * .179;
}
else if (NumExemptions == 1)
{
cout << setw(25);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << (GrossPay * .168);
FederalTax = GrossPay * .168;
}
else if (NumExemptions == 2)
{
cout << setw(25);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << (GrossPay * .157);
FederalTax = GrossPay * .157;
}
else if (NumExemptions == 3)
{
cout << setw(25);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << (GrossPay * .146);
FederalTax = GrossPay * .146;
}
else if (NumExemptions >= 4)
{
cout << setw(25);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << (GrossPay * .125);
FederalTax = GrossPay * .125;
}
cout << endl;
cout << "State Taxes";
cout << setw(27);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << GrossPay * .0825;
cout << endl;
cout << endl;
cout << "Net Pay";
cout << setw(31);
cout << (GrossPay - FederalTax - StateTax);
cout << endl;
break;
case 'M':
cout << "Input the number of exemptions:";
cin >> NumExemptions;
cout << system("cls");
cout << "Payroll information for ";
cout << LastName << ", ";
cout << FirstName << ":";
cout << endl;
cout << endl;
cout << "Hours Worked: ";
cout << setw(24);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << HoursWorked;
cout << endl;
cout << "Hourly Rate: ";
cout << setw(25);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << HourlyRate;
cout << endl;
cout << "Marital Status: ";
cout << setw(22);
cout << "Married";
cout << endl;
cout << "Number of Exemptions: ";
cout << setw(16);
cout << NumExemptions;
cout << endl;
cout << endl;
cout << "Gross Pay";
cout << setw(29);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << GrossPay;
cout << endl;
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << "Federal Taxes";
if (NumExemptions == 0)
{
cout << setw(25);
cout << (GrossPay * .151);
FederalTax = GrossPay * .151;
}
else if (NumExemptions == 1)
{
cout << setw(25);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << (GrossPay * .142);
FederalTax = GrossPay * .142;
}
else if (NumExemptions == 2)
{
cout << setw(25);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << (GrossPay * .133);
FederalTax = GrossPay * .133;
}
else if (NumExemptions == 3)
{
cout << setw(25);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << (GrossPay * .124);
FederalTax = GrossPay * .124;
}
else if (NumExemptions >= 4)
{
cout << setw(25);
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << (GrossPay * .105);
FederalTax = GrossPay * .105;
}
cout << endl;
cout << "State Taxes";
cout << setw(27);
cout << GrossPay * .0825;
cout << endl;
cout << endl;
cout << "Net Pay";
cout << setw(31);
cout << GrossPay - FederalTax - StateTax;
cout << endl;
break;
default: cout << "Invalid Status, Please type either 'M' for married
or 'S' for single";
}
return 0;
}