我正在尝试检查可接受的身高和体重,并让 c++ 编译器输出候选人被拒绝的原因
I'm trying to check for acceptable height and weight and have the c++ compiler output why a candidate is rejected
当只有权重错误时,以下C++代码不会输出任何拒绝消息。我的嵌套"如果-然后-否则"检查有什么问题?
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/C++编译器通常会删除重复的库吗
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- Win32编译器选项和内存分配
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++,我收到一个无法理解的编译器错误
- 在线编译器中的分段C++没有打印消息
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- C/C++预处理器是否可以检测一些编译器选项
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 我需要知道编译器如何在cpp中使用析构函数
- 编译器如何区分std::vector的构造函数
- CLANG 编译器 说:变量"PTR"可能未初始化
- 告诉c++编译器该参数没有别名
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 为什么所有C++编译器都会崩溃或挂起此代码
- 编译器如何在使用SFINAE的函数和标准函数之间确定两者是否可行
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 为什么C++编译器没有检测到正确声明的类?