Search code examples
c++testingpostdo-loops

Post test loop not working when using a char value


Ive been trying to get this code to work but im having trouble when i got to the Char value post test, the post test is supposed to loop only if a character that is not "Y" or "N" ("y" or "n") is inputed by the user however it just keep looping regardless of what i input was also wondering if there is a chance that this same problem could occur if i did the same do while loop down below in the insurance plan part of the code? any helps appreciated.

#include <iostream>
#include <iomanip>
using namespace std;
int main() {

    //variables
    double hours = 0;
    double rate = 0;
    double gross = 0;
    double state = 0;
    double fed = 0;
    double local = 0;
    double FICA = 0;
    double netpay = 0;
    double ins = 0;
    double otHrs = 0.0;
    double otPay = 0.0;
    double dep = 0;
    char sol;
    char fam;

    //Proccess & check
    do {                                                                    // Hours Post-Check
        cout << "Enter your hours worked: ";
        cin >> hours;
            if (hours < 0) {
                cout << "Enter a number of hours greater than 0" << endl;
            }
    } while (hours < 0);

    do {                                                                    //Pay Post-Check
        cout << "Enter your hourly pay: ";
        cin >> rate;
            if (rate < 11 || rate > 75) {
                cout << "Enter a pay rate that is greater than 11 or less than 75" << endl;
            }
    } while (rate < 11 || rate > 75);

    do {                                                                    //Dependent Post-Check
        cout << "Enter the number of dependents: ";
        cin >> dep;
            if (dep < 0) {
                cout << "Enter a number of dependents greater than 0" << endl;
            }
    } while (dep < 0);

    do {                                                                    //Healthcare Post-Check
        cout << "Healthcare? (Y/N): ";
        cin >> sol;
        if (sol != 'Y' || 'N') {                                            //Having trouble with this line of code is 
            cout << "Enter Y or N" << endl;                                 //This because I am using a Char versus a double/int?
            }                                                               //Im trying to have it loop only if sol does not equal Y or N (y or n)
    } while (sol != 'Y'|| 'N');                                             //It just continues the loop infinitely
                                                                            //will this problem be the same if i try to do this below in the insurance plan?
    //Calculating OverTime
    if (hours > 40) {
        otHrs = hours - 40;
        otPay = (rate * 1.5) * otHrs;
        gross = otPay + 40 * rate;
    }
    else {
        gross = (hours * rate);
    }


    //Dependents
    if (dep <= 2) {
        fed = 0.18 * gross;
    }
    else if (dep <= 3 && dep <= 6) {
        fed = 0.15 * gross;
    }
    else if (dep > 6) {
        fed = 0.13 * gross;
    }
    //Deductions
    state = .037 * gross;
    local = .01 * gross;
    FICA = .062 * gross;
    netpay = gross - (state + fed + local + FICA);

    //Insurance Y(y) or N(n)
    if (sol == 'Y' || sol == 'y') {                         
        cout << "Individual or Family plan (I/F): ";
        cin >> fam;

        //insurance plan
        if (fam == 'I' || fam == 'i') {
            ins = 35.00;
            netpay -= ins;
        }
        else if (fam == 'F' || fam == 'f') {
            ins = 60.00;
            netpay -= ins;
        }
    }






    cout << fixed;
    cout << setprecision(2) << endl;
    //display
    cout << "Gross pay:     $" << setw(11) << gross << endl;
    cout << "Federal tax:  " << setw(18) << fed << endl;
    cout << "State tax:  " << setw(20) << state << endl;
    cout << "local tax:  " << setw(20) << local << endl;
    cout << "FICA tax:  " << setw(21) << FICA << endl;
    cout << "Health insurance:   " << setw(12) << ins << endl;
    cout << "--------------------------------" << endl;
    cout << "Net Pay:       $" << setw(11) << netpay << endl;

    cout << endl << endl;
    return 0;
}

Solution

  • The problem is your while condition (also your if condition a few lines above the while loop) :

    while (sol != 'Y'|| 'N')
    

    When using the double pipe 'or' ||, each side is evaluated independently. The left side (assuming sol == 'Y') would evaluate to False. But the right side 'N' will always be true since the character 'N' is not 0 (False). The fix:

    while (sol != 'Y' && sol != 'N')
    

    good luck!