Not user friendly?? idk why

Hey guys,
so my program compiles and runs, but for some reason when the user inputs an employees name with a whitespace(for example "Egg Chunk" instead of just one name like "Egg" it shows a totally different output than what i want. My output should be (gross = 3575) 2579.44 as the net pay




//This program is designed to calculate the Net Pay of an Employee by deducting certain taxes

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
cout << "This program calculates and prints a montly paycheck for an employee." << endl;
cout << "The net pay is calculated after taking the following deductions." << endl;

cout << " " << endl;
cout << "Federal Income Tax: 10%" << endl;
cout << "State Tax: 3.25%" << endl;
cout << "Social Security Tax: 4.75%" << endl;
cout << "Medicare/Medicad Tax: 2.75%" << endl;
cout << "Pension Plan: 5%" << endl;
cout << "Health Insurance: $75.00" << endl;
cout << " " << endl;

float grossPay;
float netPay;
const float fedTax = 10;
const float stateTax = 3.25;
const float socialTax = 4.75;
const float mediTax = 2.75;
const float penPlan = 5;
const float healthIns = 75;
float sum;


/****ASK USER****/

string employee;
cout << "Enter Employee Name; " << endl;
cin >> employee;
cout << "Enter Gross Pay: " << endl;
cin >> grossPay;
cout << " " << endl;

/****PRINT RESULT****/

cout << fixed << setprecision(2) << endl;
cout << "Paycheck summary: " << endl;
cout << " " << endl;
cout << "Employee Name: " << employee << endl;
cout << "Gross Pay: $" << grossPay << endl;
cout << "Federal Income Tax: $" << grossPay *(fedTax/100) << endl;
cout << "State Tax: $"<< grossPay * (stateTax/100) << endl;
cout << "Social Security Tax: $" << grossPay * (socialTax/100) << endl;
cout << "Medicare/Medicaid Tax: $" << grossPay * (mediTax/100)<< endl;
cout << "Pension Plan: $" << grossPay * (penPlan/100)<< endl;
cout << "Health Insurance: $" << healthIns << endl;
sum = grossPay - ((grossPay * (fedTax/100)) + (grossPay * (stateTax/100)) + (grossPay * (socialTax/100)) + (grossPay * (mediTax/100)) + (grossPay * (penPlan/100)) + healthIns);
cout << "Net Pay: $" << sum << endl;
cout << " " << endl;
cout << " " << endl;

cout << "Prepared by ______" << endl;
cout << "September 2017" << endl;

return 0;
}
Last edited on
cin will stop reading at a whitespace. If you enter a string with a whitespace only the part before the whitespace will be accepted. The part after the whitespace will remain in the input buffer and will be read with the next input operation. If your input might contain whitespace then use getline.
I used
getline(cin, employee); to improve the result
^^thanks! it worked:)
Last edited on
Topic archived. No new replies allowed.