C++ c++;循环和布尔表达式
几天来,我在为我的班级做这项作业时遇到了困难,希望得到一些帮助 任务是编写一个程序,根据用户的身高和体重资格(取决于其性别),通知用户其接受状态 在课程结束时,它希望输出被录取的候选人数量以及被录取的候选人总数的平均值 转让- 我们不能使用开关、条件运算符和选择(仅用于向结果输出正确的消息)。我们只能使用循环和复杂的布尔表达式 我遇到的问题是:C++ c++;循环和布尔表达式,c++,eclipse,C++,Eclipse,几天来,我在为我的班级做这项作业时遇到了困难,希望得到一些帮助 任务是编写一个程序,根据用户的身高和体重资格(取决于其性别),通知用户其接受状态 在课程结束时,它希望输出被录取的候选人数量以及被录取的候选人总数的平均值 转让- 我们不能使用开关、条件运算符和选择(仅用于向结果输出正确的消息)。我们只能使用循环和复杂的布尔表达式 我遇到的问题是: 如果我的3个输入都是有效的,如果它们被接受,为什么它们不输出?如果其中一个输入(身高或体重)或两者都被拒绝,为什么它不输出。我的布尔变量不正确吗?如果是
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char gender;
int height;
int weight;
bool heightOK;
bool weightOK;
int candidateCount;
int validCandidateCount;
bool invalidGender;
bool invalidHeight;
bool invalidWeight;
float percentOutput;
candidateCount = 0;
validCandidateCount = 0;
cout << "Please enter the candidate's information (enter 'X' to exit)."
<< endl;
do
{
cout << "Gender: ";
cin.get(gender);
cin.ignore (1000,'\n');
invalidGender = ( !(gender == 'm' ||
gender == 'M' ||
gender == 'f' ||
gender == 'F' ));
candidateCount = candidateCount + 1;
if(invalidGender)
{
cout << "***** Invalid gender; please enter M or F*****" << endl;
}
}while(invalidGender);
while (gender != 'X' || gender != 'x')
{
candidateCount = candidateCount + 1;
do
{
cout << "Height: ";
cin >> height;
invalidHeight = height < 24 || height > 110;
heightOK = ((gender == 'm' || gender == 'M') &&
(height > 65 && height < 80));
heightOK = heightOK || ((gender == 'f' || gender == 'F') &&
(height > 62 && height < 75));
if(invalidHeight)
{
cout << "***** Invalid height; please enter a height in inches between 24 and 110. *****"
<< endl;
}
}while(invalidHeight);
do
{
cout << "Weight: ";
cin >> weight;
invalidWeight = weight < 50 || weight > 1400;
weightOK = ((gender == 'm' || gender == 'M') &&
(weight > 130 && weight < 250));
weightOK = weightOK || ((gender == 'f' || gender == 'F') &&
(weight > 110 && weight < 185));
if(invalidWeight)
{
cout << "***** Invalid weight; please enter a weight in lbs between 50 and 1400."
<< endl;
}
}while(invalidWeight);
if(heightOK && weightOK)
{
cout << "This candidate has been ACCEPTED!" << endl;
validCandidateCount = validCandidateCount + 1;
}
else if (!heightOK)
{
cout << "This candidate has been rejected based on the HEIGHT requirement."
<< endl;
}
else if (!weightOK)
{
cout << "This candidate has been rejected based on the WEIGHT requirement."
<< endl;
}
else if (!(heightOK && weightOK))
{
cout << "This candidate has been rejected based on the HEIGHT and WEIGHT requirements"
<< endl;
}
do
{
cout << "Gender: ";
cin.get(gender);
cin.ignore (1000,'\n');
candidateCount = candidateCount + 1;
if(invalidGender)
{
cout << "***** Invalid gender; please enter M or F*****" << endl;
}
}while(invalidGender);
}
cout << validCandidateCount << " candidate(s) accepted!" << endl;
percentOutput = validCandidateCount / candidateCount;
cout << "That's " << percentOutput <<"%!" << endl;
return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
性别;
内部高度;
整数权重;
布尔·海托克;
bool-weightOK;
int候选计数;
int validCandidateCount;
布尔残疾性别;
布尔残疾身高;
体重超标;
浮动百分比输出;
候选人数=0;
validCandidateCount=0;
库特130(重量<250);
weightOK=weightOK | |((性别='f'| |性别='f')&&
(重量>110,重量<185);
if(无效重量)
{
cout主while回路应具备以下条件:
while(gender !='X' && gender!='x)
并且您的选择代码有错误的条件语句
if(heightOK && weightOK)
{
cout << "This candidate has been ACCEPTED!" << endl;
validCandidateCount = validCandidateCount + 1;
}
else if (!heightOK) // you have written else if(heightOK)
{
cout << "This candidate has been rejected based on the HEIGHT requirement."
<< endl;
}
else if (!weightOK) // you have written else if(weightOK)
{
cout << "This candidate has been rejected based on the WEIGHT requirement."
<< endl;
}
else if (!(heightOK && weightOK))
{
cout << "This candidate has been rejected based on the HEIGHT and WEIGHT requirements"
<< endl;
}
全部代码
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char gender;
int height;
int weight;
bool heightOK;
bool weightOK;
int candidateCount;
int validCandidateCount;
bool invalidGender;
bool invalidHeight;
bool invalidWeight;
double percentOutput;
candidateCount = 0;
validCandidateCount = 0;
cout << "Please enter the candidate's information (enter 'X' to exit)."
<< endl;
cout << "Gender: ";
cin.get(gender);
while (gender != 'X' && gender != 'x')
{
candidateCount = candidateCount + 1;
do
{
invalidGender = ( !(gender == 'm' ||
gender == 'M' ||
gender == 'f' ||
gender == 'F' ));
if(invalidGender)
{
cout << "***** Invalid gender; please enter M or F*****" << endl;
cout << "Gender: ";
cin>>gender;
cin.ignore (1000,'\n');
}
}while(invalidGender);
do
{
cout << "Height: ";
cin >> height;
invalidHeight = height < 24 || height > 110;
heightOK = ((gender == 'm' || gender == 'M') &&
(height > 65 && height < 80));
heightOK = heightOK || ((gender == 'f' || gender == 'F') &&
(height > 62 && height < 75));
if(invalidHeight)
{
cout << "***** Invalid height; please enter a height in inches between 24 and 110. *****"
<< endl;
}
}while(invalidHeight);
do
{
cout << "Weight: ";
cin >> weight;
invalidWeight = weight < 50 || weight > 1400;
weightOK = ((gender == 'm' || gender == 'M') &&
(weight > 130 && weight < 250));
weightOK = weightOK || ((gender == 'f' || gender == 'F') &&
(weight > 110 && weight < 185));
if(invalidWeight)
{
cout << "***** Invalid weight; please enter a weight in lbs between 50 and 1400."
<< endl;
}
}while(invalidWeight);
if(heightOK && weightOK)
{
cout << "This candidate has been ACCEPTED!" << endl;
validCandidateCount = validCandidateCount + 1;
}
else if (!heightOK)
{
cout << "This candidate has been rejected based on the HEIGHT requirement."
<< endl;
}
else if (!weightOK)
{
cout << "This candidate has been rejected based on the WEIGHT requirement."
<< endl;
}
else if (!(heightOK && weightOK))
{
cout << "This candidate has been rejected based on the HEIGHT and WEIGHT requirements"
<< endl;
}
cout << "Gender: ";
cin>>gender;
cin.ignore (1000,'\n');
}
cout << validCandidateCount << " candidate(s) accepted!" << endl;
percentOutput = (double)validCandidateCount / (double)candidateCount;
cout << "That's " << percentOutput*100 <<"%!" << endl;
return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
性别;
内部高度;
整数权重;
布尔·海托克;
bool-weightOK;
int候选计数;
int validCandidateCount;
布尔残疾性别;
布尔残疾身高;
体重超标;
双百分比输出;
候选人数=0;
validCandidateCount=0;
cout 62&&高度<75);
if(无效高度)
{
库特1400;
权重OK=((性别='m'| |性别='m')&&
(重量>130,重量<250);
weightOK=weightOK | |((性别='f'| |性别='f')&&
(重量>110,重量<185);
if(无效重量)
{
我有个坏消息。除了任何bug之外,你的键盘似乎也坏了。TAB键不能正常工作。因此,显示的代码缺乏逻辑性、有意义的缩进,并且大部分不可读。你需要修复你的键盘。毕竟,如果你要求其他人帮助解决你的编程问题,至少你可以通过使用逻辑缩进来尽可能地让你的代码易于阅读和理解。如果你连正确缩进代码的麻烦都没有,为什么会有人想帮你?@SamVarshavchik现在修好了吗?不知道发布时不能使用tab。所以我只使用了4个空格。你在哪里找到了您声明了invalidGender
?当我运行您的代码时,编译器告诉我错误:|124;===生成文件:“无项目”中的“无目标”(编译器:未知)===| | | |函数“int main()”:| | 31 |错误:“invalidGender”未在此范围内声明| | 42 |错误:'invalidGender'未在此范围内声明| | | | | | |===生成失败:2个错误,0次警告(0分1秒)===|
`@abusycomgrammer抱歉,我刚刚编辑了它,不知怎么的,当我复制和粘贴我的代码时,它没有把它带进来。现在应该修复它。这似乎更像是一个逻辑错误。我建议你玩弄一下你的逻辑;分别尝试每个do while语句,看看它是否提供了你想要/期望它提供的。Hopef很抱歉,您在到期前还有一段时间;)嗨,Chandini,谢谢您的回复。我在第二次性别输入中再次添加了invalidGender变量赋值(上次执行)但是我不明白我应该把上一个do-while中的无效性别条件放在哪里。如果我在上一个do-while中删除了无效的条件,我应该添加什么呢?因为我不能把do-while中的while条件保留为空。我该如何将它添加到主while循环的开始?好的,我这样做了,但现在我的程序结束了在我将有效的genderInput放在高度的do while循环前面之后,添加do{if(invalidGender){如果没有删除无效的条件,我会很感谢。我该怎么做?我不能将do while条件保留为空,或者它会给我一个语法错误。就像你说的,它会执行无限循环,输入X不会使我退出。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char gender;
int height;
int weight;
bool heightOK;
bool weightOK;
int candidateCount;
int validCandidateCount;
bool invalidGender;
bool invalidHeight;
bool invalidWeight;
double percentOutput;
candidateCount = 0;
validCandidateCount = 0;
cout << "Please enter the candidate's information (enter 'X' to exit)."
<< endl;
cout << "Gender: ";
cin.get(gender);
while (gender != 'X' && gender != 'x')
{
candidateCount = candidateCount + 1;
do
{
invalidGender = ( !(gender == 'm' ||
gender == 'M' ||
gender == 'f' ||
gender == 'F' ));
if(invalidGender)
{
cout << "***** Invalid gender; please enter M or F*****" << endl;
cout << "Gender: ";
cin>>gender;
cin.ignore (1000,'\n');
}
}while(invalidGender);
do
{
cout << "Height: ";
cin >> height;
invalidHeight = height < 24 || height > 110;
heightOK = ((gender == 'm' || gender == 'M') &&
(height > 65 && height < 80));
heightOK = heightOK || ((gender == 'f' || gender == 'F') &&
(height > 62 && height < 75));
if(invalidHeight)
{
cout << "***** Invalid height; please enter a height in inches between 24 and 110. *****"
<< endl;
}
}while(invalidHeight);
do
{
cout << "Weight: ";
cin >> weight;
invalidWeight = weight < 50 || weight > 1400;
weightOK = ((gender == 'm' || gender == 'M') &&
(weight > 130 && weight < 250));
weightOK = weightOK || ((gender == 'f' || gender == 'F') &&
(weight > 110 && weight < 185));
if(invalidWeight)
{
cout << "***** Invalid weight; please enter a weight in lbs between 50 and 1400."
<< endl;
}
}while(invalidWeight);
if(heightOK && weightOK)
{
cout << "This candidate has been ACCEPTED!" << endl;
validCandidateCount = validCandidateCount + 1;
}
else if (!heightOK)
{
cout << "This candidate has been rejected based on the HEIGHT requirement."
<< endl;
}
else if (!weightOK)
{
cout << "This candidate has been rejected based on the WEIGHT requirement."
<< endl;
}
else if (!(heightOK && weightOK))
{
cout << "This candidate has been rejected based on the HEIGHT and WEIGHT requirements"
<< endl;
}
cout << "Gender: ";
cin>>gender;
cin.ignore (1000,'\n');
}
cout << validCandidateCount << " candidate(s) accepted!" << endl;
percentOutput = (double)validCandidateCount / (double)candidateCount;
cout << "That's " << percentOutput*100 <<"%!" << endl;
return 0;
}