Reputation: 122
This program works perfect on the first iteration, but on any iteration after, the second cin is skipped ans the message displays. Also, in the second iteration, if the name is two parts, like first and last name, then only the first will display.
Any help?
double calcGrossPay (double payRate, double hours);
double nPay (double& fedTx, double& localTx, double& stateTx, double& ssTx, double& netPay, double fPay);
void displayAll (double fPay, double netPay, string name);
double fedTx = 14, stateTx = 6, localTx = 3.5, ssTx = 4.75;
int main()
{
while (!cin.eof())
{
string name;
cout <<"Please enter your working name: ";
cin >> name;
if (cin.eof())
return 0;
double payRate, hours;
cout <<"Enter your pay rate and hours worked, respectively."<< endl;
cin >> payRate >> hours;
if (cin.eof())
return 0;
double fPay = calcGrossPay (payRate, hours);
double netPay = 0;
netPay = nPay (fedTx, localTx, stateTx, ssTx, netPay, fPay);
displayAll (fPay, netPay, name);
system("pause");
}
}
double calcGrossPay (double payRate, double hours)
{
double extraT, fPay;
if (hours > 40)
{
extraT = (hours - 40) * (1.5 * payRate);
fPay = extraT + (40 * payRate);
}
else
fPay = payRate * hours;
return fPay;
}
double nPay (double& fedTx, double& localTx, double& stateTx, double& ssTx, double& netPay, double fPay)
{
double totalTx = fedTx + localTx + stateTx + ssTx;
netPay = fPay * (1 - (totalTx / 100));
return netPay;
}
void displayAll (double fPay, double netPay, string name)
{
cout <<"Below is "<< name << "'s salary information" << endl;
cout << fixed << showpoint << setprecision(2) <<"\nYour calculated gross pay is $"
<< fPay << ", and your net pay is $" << netPay << endl;
}
Upvotes: 1
Views: 514
Reputation: 33566
cin >>
operator reads data only upto the first whitespace character.
So, if your data-reading is cin>> name; cin>>payRate; cin >> hours
, and if you enter John Doe 12 34
, then "John" will be read as the name, and "doe" will be read as the payRate. At this moment the data-reading will fail, since doe
cannot be transformed into any numeric value.
Also, at this point the cin
stream will enter into an error state
upon which it will cease to read any further data. All attempts to read will simply be skipped. Therefore the reading cin>>hours
will be skipped, and all reading in the next loop iteration will be also skipped. And any other data-reading from cin
that you have in your app.
But you always can (and should!) check if there were any errors. You can check cin.good
or cin.fail
error flags so you can detect if the stream has entered into error state. Search on google or StackOverflow for "cin error handling" and you will quickly find examples.
After you detect the error, you can reset the stream by cin.clear()
. This does not remove any data from the stream - it just clears the error flags so that the stream starts reading the data again. Mind that the old data is NOT removed. If after failing on cin >> payrate
due to Doe
you'd reset the cin, then the next data read would be doe
again.
The last part of your problem is reading the string "name" so that it can include whitespaces. As I said, cin >>
reads only upto the first whitespace, so it is hardlly usable here.
Instead, switch to reading a whole line of text through std::getline
function. It's used like this:
std::getline( cin, name )
It will read whole line of text as the name, including any spaces, tabs etc. It will end reading on \n, \r or \r\n characters, depending on your OS.
Upvotes: 2