Пост тестовый цикл не работает при использовании значения char

Я пытался заставить этот код работать, но у меня возникли проблемы, когда я добрался до пост-теста значения Char, пост-тест должен зацикливаться, только если символ, который не является «Y» или «N» («y» или » n «) вводится пользователем, однако он просто продолжает цикл, независимо от того, что я вводил, также задавался вопросом, есть ли вероятность того, что эта же проблема может возникнуть, если бы я сделал то же самое, сделав пока цикл внизу в части кода плана страхования ? Любая помощь приветствуется.

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

0

Решение

Проблема в том, что вы выполняете условие while (также ваше условие if на несколько строк выше цикла while):

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

При использовании двойной трубы «или» ||каждая сторона оценивается независимо. Левая сторона (при условии, что sol == ‘Y’) оценивается как False. Но с правой стороны 'N' всегда будет истинным, так как символ ‘N’ не равен 0 (Ложь). Исправление:

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

удачи!

0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector