I\'m trying to configure this C++ program, to when a user inputs another value o
ID: 3536393 • Letter: I
Question
I'm trying to configure this C++ program, to when a user inputs another value other than a number for gross pay ie a letter or a simbol, it says Error: invalid gross pay amount. So far it only does that for negative numbers only. Here is the program:
#include <iostream> //For cin and cout
#include <string> //may include letters, digits, and various special characters
#include <iomanip> //Define several manipulators that each take a single argument
using namespace std;
int main()
{
/*
outputs
Federal Income Tax , State Tax , Social Security + Medicare Tax , Health Insurance
*/
string empName = "";
double grossPay = -1 , federalTax , stateTax , sSecMedcare , healthIns ;
cout << "Enter Employee's name "<<endl;
getline(cin,empName);
cout << "Enter Gross pay " <<endl;
cin >> grossPay ;
if(grossPay < 0)
{
cout << " ERROR: Invalid gross pay amount " <<endl;
cout<< "Press any key to exit." << endl;
cin.ignore(2);
return 0;
}
cout << std::fixed << std::setprecision(2) ;
cout<< empName <<endl;
cout<< "Gross Amount: ............$" << grossPay << endl;
//input to o/p processing
federalTax = ( .15 * grossPay ) ;
stateTax = ( .035 * grossPay ) ;
sSecMedcare = ( .085 * grossPay );
healthIns = 75;
cout<< "Federal Tax: .............$" << federalTax << endl;
cout <<"State Tax:................$" << stateTax << endl;
cout<< "Social sec/Medicare:------$" << sSecMedcare << endl;
cout<< "Health Insurance:.........$" << healthIns << endl;
cout <<"Net pay:..................$" << ( grossPay - (federalTax + stateTax + sSecMedcare + healthIns)) <<endl;
system("PAUSE");
}
THANK YOU.
Explanation / Answer
You can check to see if an int has been filled with this code: