The assignment is to make a program where the gross pay is input and to display
ID: 3643637 • Letter: T
Question
The assignment is to make a program where the gross pay is input and to display all deductions and netpay after deductions. The part of the program I am having trouble with is this"Your program should deal with erroneous input values. Gross salary should always be a positive number. Make sure that you deal with the possibility that the user may have entered a non-numeric input value. Have your program output appropriate error messages in these cases."
I have to add a switch statement to my program but I don't know where to add it in and what to name the cases. Here is my program:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const double FED_TAX = .15;
const double STATE_TAX = .035;
const double SOC_MED_TAX = .085;
int main()
{
string name;
string name2;
double netPay, fedTax, grossPay, stateTax, socMedTax;
double healthInsu = 75.00;
char money = '$';
char space = ' ';
cout << "What is your first and last name?" << endl;
cin >> name >> name2;
cout << "What is your Gross Pay this month?" << endl;
cin >> grossPay;
fedTax = grossPay * FED_TAX;
stateTax = grossPay * STATE_TAX;
socMedTax = grossPay * SOC_MED_TAX;
netPay = grossPay - (fedTax + stateTax + socMedTax + healthInsu);
cout << name << space << name2 << endl;
cout << setprecision (2);
cout << "Gross Amount: " << setfill ('.') << setw (12);
cout << space << money << grossPay << endl;
cout << "Federal Tax: " << setfill ('.') << setw (13);
cout << space << money << fedTax << endl;
cout << "State Tax: " << setfill ('.') << setw (15);
cout << space << money << stateTax << endl;
cout << "Social Security + Medicare Tax: " << setfill ('.') << setw (3);
cout << space << money << socMedTax << endl;
cout << "Health Insurance: " << setfill ('.') << setw (8);
cout << space << money << healthInsu << endl;
cout << "Net Pay: " << setfill ('.') << setw (17);
cout << space << money << netPay << endl;
system ("pause");
return 0;
}
Explanation / Answer
What i am doing is a stupid way of using Swtich statement. But this is what i could think of. I saw this same question in electrical section. Something was mentioned regarding error handling. Thats where i thot of using switch. Have a look: The program works perfectly fine :) #include <iostream>#include <cstdlib>
#include <string>
#include <iomanip>
using namespace std;
const double FED_TAX = .15;
const double STATE_TAX = .035;
const double SOC_MED_TAX = .085;
int main()
{
string name;
cout << "Enter Employee Name : ";
getline(cin, name);
double grossPay;
int err = 1;
while (err != 0) {
cout << "Enter Gross Pay: ";
if (!(cin >> grossPay))
{err = 1;
cin.clear();
cin.sync() ;
}
else
{if (grossPay < 0) err = 2; else err = 0;
}
switch (err){
case 1 :
cout << "Please enter a numeric amount. ";
break;
case 2 :
cout << "Please enter a positive amount. ";
break;
default :
break;
}
}
double netPay, fedTax, stateTax, socMedTax;
double healthInsu = 75.00;
fedTax = grossPay * FED_TAX;
stateTax = grossPay * STATE_TAX;
socMedTax = grossPay * SOC_MED_TAX;
netPay = grossPay - (fedTax + stateTax + socMedTax + healthInsu);
cout << name << endl;
cout << showpoint << setprecision(2);
cout << left << setfill('.') << setw(30) << "Gross Pay: " << right << setfill(' ') << " $" << setw(9) << fixed << grossPay << endl;
cout << left << setfill('.') << setw(30) << "Federal Tax: " << right << setfill(' ') << " $" << setw(9) << fixed << fedTax << endl;
cout << left << setfill('.') << setw(30) << "State Tax: " << right << setfill(' ') << " $" << setw(9) << fixed << stateTax << endl;
cout << left << setfill('.') << setw(30) << "Social Sec / Medicare: " << right << setfill(' ') << " $" << setw(9) << fixed << socMedTax << endl;
cout << left << setfill('.') << setw(30) << "Health Insurance: " << right << setfill(' ') << " $" << setw(9) << fixed << healthInsu << endl;
cout << left << setfill('.') << setw(30) << "Net Pay: " << right << setfill(' ') << " $" << setw(9) << fixed << netPay << endl;
system ("pause");
return 0;
}
What i am doing is a stupid way of using Swtich statement. But this is what i could think of. I saw this same question in electrical section. Something was mentioned regarding error handling. Thats where i thot of using switch. Have a look: The program works perfectly fine :) #include <iostream> #include <cstdlib> #include <string> #include <iomanip> using namespace std; const double FED_TAX = .15; const double STATE_TAX = .035; const double SOC_MED_TAX = .085; int main() { string name; cout << "Enter Employee Name : "; getline(cin, name); double grossPay; int err = 1; while (err != 0) { cout << "Enter Gross Pay: "; if (!(cin >> grossPay)) {err = 1; cin.clear(); cin.sync() ; } else {if (grossPay < 0) err = 2; else err = 0; } switch (err){ case 1 : cout << "Please enter a numeric amount. "; break; case 2 : cout << "Please enter a positive amount. "; break; default : break; } } double netPay, fedTax, stateTax, socMedTax; double healthInsu = 75.00; fedTax = grossPay * FED_TAX; stateTax = grossPay * STATE_TAX; socMedTax = grossPay * SOC_MED_TAX; netPay = grossPay - (fedTax + stateTax + socMedTax + healthInsu); cout << name << endl; cout << showpoint << setprecision(2); cout << left << setfill('.') << setw(30) << "Gross Pay: " << right << setfill(' ') << " $" << setw(9) << fixed << grossPay << endl; cout << left << setfill('.') << setw(30) << "Federal Tax: " << right << setfill(' ') << " $" << setw(9) << fixed << fedTax << endl; cout << left << setfill('.') << setw(30) << "State Tax: " << right << setfill(' ') << " $" << setw(9) << fixed << stateTax << endl; cout << left << setfill('.') << setw(30) << "Social Sec / Medicare: " << right << setfill(' ') << " $" << setw(9) << fixed << socMedTax << endl; cout << left << setfill('.') << setw(30) << "Health Insurance: " << right << setfill(' ') << " $" << setw(9) << fixed << healthInsu << endl; cout << left << setfill('.') << setw(30) << "Net Pay: " << right << setfill(' ') << " $" << setw(9) << fixed << netPay << endl; system ("pause"); return 0; }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.