我正在尝试检查可接受的身高和体重,并让 C++ 编译器输出候选人被拒绝的原因
I'm trying to check for acceptable height and weight and have the c++ compiler output why a candidate is rejected
只有重量错误时,以下C++代码不输出任何拒绝信息。我的嵌套 if-then-else 检查有什么问题?
char gender; //INPUT Gender of candidate
char genderOk; //INPUT Checks validity of input for each value
int height; //INPUT Height of candidate
int heightOk; //INPUT Range of valid heights for candidates
int weight; //INPUT Weight of candidate
int weightOk; //INPUT Range of valid weights for candidates
// INPUT - describe input here
cout << left;
cout << "Please enter candidate's information (enter 'X' to "
"exit).\n";
do {
cout << "Gender: ";
cin.get(gender);
cin.ignore(1000, '\n');
genderOk = gender != 'm' && gender != 'M' && gender != 'f' && gender != 'F';
if(genderOk) {
cout << "***** Invalid gender; please enter M or F *****";
cout << endl;
}
} while(genderOk);
do {
cout << "Height: ";
cin >> (height);
cin.ignore(1000, '\n');
heightOk = height >=24 && height <= 110;
if (!heightOk) {
cout << "***** Invalid height; please enter a height in "
"inches between 24 and 110. *****";
cout << endl;
}
} while(!heightOk);
do {
cout << "Weight: ";
cin >> weight;
cin.ignore(1000, '\n');
weightOk = weight >= 50 && weight <= 1400;
if(!weightOk) {
cout << "***** Invalid weight; please enter a weight in "
"lbs between 50 and 1400. *****";
cout << endl;
}
} while(!weightOk);
if (gender == 'm' || gender == 'M') {
heightOk = height >= 65 && height <= 80;
weightOk = weight >= 130 && weight <= 250;
if (heightOk && weightOk) {
cout << RESULT << "ACCEPTED!";
}
else {
if (!heightOk) {
if (!weightOk) {
cout << RESULT << "rejected based on the "
"HEIGHT and WEIGHT "
"requirements.";
}
else {
if(!weightOk) {
cout << RESULT << "rejected based on the"
" WEIGHT requirement.";
}
else {
cout << RESULT << "rejected based on the "
"HEIGHT requirement.";
}
}
}
}
}
if (gender == 'f' || gender == 'F') {
heightOk = height >= 62 && height <=75;
weightOk = weight >= 110 && weight <= 185;
if (heightOk && weightOk) {
cout << RESULT << "ACCEPTED!";
}
else {
if (!heightOk) {
if (!weightOk) {
cout << RESULT << "rejected based on the "
"HEIGHT and WEIGHT "
"requirements.";
}
else {
if (!weightOk) {
cout << RESULT << "rejected based on the"
" WEIGHT requirement.";
}
else {
cout << RESULT << "rejected based on the "
"HEIGHT requirement.";
}
}
}
}
}
如果 weightOk 不为假则为真:
if(!weightOk) {
// weightOk=0
} else {
// weightOk=1 then
if(!weightOk) {
// never reaches here if weightOk=0
}
}
只有重量错误时,以下C++代码不输出任何拒绝信息。我的嵌套 if-then-else 检查有什么问题?
char gender; //INPUT Gender of candidate
char genderOk; //INPUT Checks validity of input for each value
int height; //INPUT Height of candidate
int heightOk; //INPUT Range of valid heights for candidates
int weight; //INPUT Weight of candidate
int weightOk; //INPUT Range of valid weights for candidates
// INPUT - describe input here
cout << left;
cout << "Please enter candidate's information (enter 'X' to "
"exit).\n";
do {
cout << "Gender: ";
cin.get(gender);
cin.ignore(1000, '\n');
genderOk = gender != 'm' && gender != 'M' && gender != 'f' && gender != 'F';
if(genderOk) {
cout << "***** Invalid gender; please enter M or F *****";
cout << endl;
}
} while(genderOk);
do {
cout << "Height: ";
cin >> (height);
cin.ignore(1000, '\n');
heightOk = height >=24 && height <= 110;
if (!heightOk) {
cout << "***** Invalid height; please enter a height in "
"inches between 24 and 110. *****";
cout << endl;
}
} while(!heightOk);
do {
cout << "Weight: ";
cin >> weight;
cin.ignore(1000, '\n');
weightOk = weight >= 50 && weight <= 1400;
if(!weightOk) {
cout << "***** Invalid weight; please enter a weight in "
"lbs between 50 and 1400. *****";
cout << endl;
}
} while(!weightOk);
if (gender == 'm' || gender == 'M') {
heightOk = height >= 65 && height <= 80;
weightOk = weight >= 130 && weight <= 250;
if (heightOk && weightOk) {
cout << RESULT << "ACCEPTED!";
}
else {
if (!heightOk) {
if (!weightOk) {
cout << RESULT << "rejected based on the "
"HEIGHT and WEIGHT "
"requirements.";
}
else {
if(!weightOk) {
cout << RESULT << "rejected based on the"
" WEIGHT requirement.";
}
else {
cout << RESULT << "rejected based on the "
"HEIGHT requirement.";
}
}
}
}
}
if (gender == 'f' || gender == 'F') {
heightOk = height >= 62 && height <=75;
weightOk = weight >= 110 && weight <= 185;
if (heightOk && weightOk) {
cout << RESULT << "ACCEPTED!";
}
else {
if (!heightOk) {
if (!weightOk) {
cout << RESULT << "rejected based on the "
"HEIGHT and WEIGHT "
"requirements.";
}
else {
if (!weightOk) {
cout << RESULT << "rejected based on the"
" WEIGHT requirement.";
}
else {
cout << RESULT << "rejected based on the "
"HEIGHT requirement.";
}
}
}
}
}
如果 weightOk 不为假则为真:
if(!weightOk) {
// weightOk=0
} else {
// weightOk=1 then
if(!weightOk) {
// never reaches here if weightOk=0
}
}