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
  • 117
  • 2
  • 12

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();
    }
}
0x499602D2
  • 87,005
  • 36
  • 149
  • 233