1

So everything in my program seems to be working fine up to the point when there's an error within one of the inputted variables.

Ex. gross pay. I need to re-input the employee number, but it keeps giving the one already given at the start. I'm not sure how to make it possible to re-input the employee number instead of having it already outputted. I can only use loops and if statements. I can't do arrays.

Please review below code

{
const int QUIT = 0, NO_NEG = 0;
int empNum = 1;
double grossPay, fedWith,
    stateWith, ficaWith, sum,
    totalGross = 0, totalFed = 0,
    totalState = 0, totalFica = 0,
    netPay, totalNetPay = 0;


cout << "Enter the following information:\n\n"
    << "Employee Number (0 to quit): ";
cin >> empNum;

while (empNum != QUIT)
{
    do
    {
        cout << "Gross pay: ";
        cin >> grossPay;

        while (grossPay < NO_NEG)
        {
            cout << "Gross pay may not be less than zero.\n";
            cout << "Re-enter Gross pay: ";
            cin >> grossPay;
        }

        cout << "Federal Withholding: ";
        cin >> fedWith;

        while (fedWith < NO_NEG)
        {
            cout << "Federal witholding may not "
                << "be less than zero.\n";
            cout << "Re-enter Federal Withholding: ";
            cin >> fedWith;
        }

        cout << "State Withholding: ";
        cin >> stateWith;

        while (stateWith < NO_NEG)
        {
            cout << "State witholding may not "
                << "be less than zero.\n";
            cout << "Re-enter State Withholding: ";
            cin >> stateWith;
        }

        cout << "FICA Withholding: ";
        cin >> ficaWith;

        while (ficaWith < NO_NEG)
        {
            cout << "FICA witholding may not be less than zero.\n";
            cout << "Re-enter FICA Withholding: ";
            cin >> ficaWith;
        }

        sum = (stateWith + fedWith + ficaWith);

        if (sum > grossPay)
        {
            cout << "\nERROR: Withholdings cannot"
                << " exceed gross pay.\n"
                << "\nPlease re-enter the data for this employee.\n"
                << "Processing the next employee:\n"
                << "\nEmployee Number (0 to quit): "
                << empNum << endl;
        }

        cout << "Processing the next employee:\n";

    } while (sum > grossPay);

    totalGross += grossPay;
    totalFed += fedWith;
    totalState += stateWith;
    totalFica += ficaWith;

    netPay = grossPay - sum;

    totalNetPay += netPay;

    cout << "Employee Number (0 to quit): ";
    cin >> empNum;

}

cout << "Total Gross Pay: $ " << totalGross << endl
    << "Total Federal Tax: $ " << totalFed << endl
    << "Total State Tax: $ " << totalState << endl
    << "Total FICA: $ " << totalFica << endl
    << "Total Net Pay: $ " << totalNetPay << endl;

return 0;
}
3
  • Did you debug the code? Commented Oct 10, 2018 at 7:14
  • Think about what you're printing and when you're printing it. You keep printing "Processing the next employee" when that's not what the code is doing. You're also asking for input when the program isn't expecting any. Commented Oct 10, 2018 at 7:17
  • Yes I did. I tried doing different ways, but I'm stuck. I'm still fairly new at coding so I don't know if the solution is simple Commented Oct 10, 2018 at 7:19

1 Answer 1

1

You already are reinputting it at the bottom. When you ask:

cout << "Employee Number (0 to quit): ";
cin >> empNum;

My suggestion would be to use getline(cin, string_name). Switch your empNum variable to a C-string. It's a type of array, but it's also a string.

Move your first cout and cin statements from above the first while loop to inside of the do-while loop. Like this:

 while (empNum != "0")
 { 
     do { 
     cout << "Enter the following information:\n\n"
     << "Employee Number (0 to quit): ";
     getline(cin, empNum);

Also, reassign the variable empNum from

 int empNum = 1;

to

 string empNum;

and also, down by the:

 cout << "Processing the next employee:\n";

place these two line below it, it'll clear the buffer, which I assume is what you want to do when reinputting variables.

 cin.clear();
 cin.ignore();

Aside from that, the simplest way to reinput variables, is simply to ask for them. Also, are you talking about reinputting the variable after the inputter hits one of your checks for false information. If so, you are reentering data for the incorrect employee data already entered. Good luck with your program!

You could use this ^^^ although I have found that there are some pesky issues with string comparison. const char and chars cant be compared strings cant be compared to chars so you'll get a lot of errors.

Your description says you need the variable to be reentered, not already outputted. The empNum variable is being reentered as long as:

 (empNum != to QUIT)

is true. The only time your variable is reinputted is if your employee data is incorrect.

Sign up to request clarification or add additional context in comments.

2 Comments

This worked, however now whenever I input 0 the program continues to loop. I'm not sure how to create a loop where when 0 is entered it stops the loop.
The problem is that we are comparing an integer to a string in that first while loop. So what you will need to do is use strstr/strcmp to compare strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.