yvflowers
yvflowers

Reputation: 13

How to re-input a variable outside of a loop

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;
}

Upvotes: 1

Views: 87

Answers (1)

Thomas
Thomas

Reputation: 2942

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.

Upvotes: 1

Related Questions