0

For some reason, the second time this loop is iterated, the cout statements overlap. In other words, after the first cout, the program does not wait for an input. How do I solve this?

Also, in the case of real life taxes, does the nPay func work right? Someone told me that the taxes should be multiplied by gross, eachindividually, and added. However, my method would work the same, especially since they occur simultaneously.

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;

          //cin.ignore();
          cout <<"Please enter your working name: ";
          getline (cin, name);
          !cin.eof();

          double payRate, hours;

          cout <<"Enter your pay rate and hours worked, respectively."<< endl;
          cin >> payRate >> hours;
          !cin.eof();

          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;
}
matttm
  • 124
  • 1
  • 2
  • 15

1 Answers1

2

After getline, a new line is still in the stream, so you will have to ignore it:

getline(cin, name);
cin.ignore();

Also, instead of while (!cin.eof()), do the extract before checking the stream:

while (getline(cin, name))
{
    cin.ignore();
    // ...
}

Here is the updated code. I hope it works for you:

int main()
{
    for (std::string name; (cout << "Please enter your working name: ") &&
                            getline(cin >> std::ws, name);)
    {
        if (cin.eof())
            break;

        double payRate, hours;

        cout << "\nEnter your pay rate and hours worked, respectively." << endl;

        if (!(cin >> payRate >> hours))
            break;

        double fPay = calcGrossPay(payRate, hours);

        double netPay = nPay(fedTx, localTx, stateTx, ssTx, netPay, fPay);

        displayAll(fPay, netPay, name);
        cin.get();
    }
}
David G
  • 94,763
  • 41
  • 167
  • 253
  • The ignore worked thx, but I don't understand where eof goes. I thought it went after every cin because when the user enters ctrl-z, the prog exits – matttm Dec 05 '13 at 12:29
  • @matttm You don't need the `!eof()`. Give me an example of the input given to the program and I will show you what to do. – David G Dec 05 '13 at 19:41
  • *code* cout <<"Please enter your working name: "; getline (cin, name); cin.ignore(); cin.eof(); *code* The eof is so when one enters ctrlZ, the prog ends. – matttm Dec 05 '13 at 21:39
  • @matttm Please check the update and tell me if it works for you. – David G Dec 05 '13 at 21:55
  • @matttm Oops! I made a mistake in my original code. I fixed it just now though. :D – David G Dec 05 '13 at 22:39