gpt4 book ai didi

C++ 循环和 bool 表达式

转载 作者:行者123 更新时间:2023-11-30 05:15:15 24 4
gpt4 key购买 nike

我已经有几天无法尝试为我的类(class)完成这项作业了,我需要一些帮助。

作业是编写一个程序,根据用户的高度和体重资格(取决于他们的性别)通知用户他们的接受状态。

在程序结束时,它希望输出被接受的候选人数量以及被接受的候选人总数与候选人总数的平均值。

作业 - https://www.saddleback.edu/faculty/slinker/CS1A/CS1A_Fall2013/Assignment8.pdf

我们不能使用开关、条件运算符和选择(仅用于向结果输出正确的消息)。我们只能使用循环和复杂的 bool 表达式

我遇到的问题是:

  1. 如果我的所有 3 个输入都有效,如果它们被接受,为什么它们不输出,如果输入之一(高度或体重)或两个都被拒绝,那么为什么不输出它。我的 bool 变量不正确吗?如果是这样,我该如何解决。

  2. 为什么我输入 X 后无法退出循环/程序,我的 while 循环是否正确?

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

最佳答案

主 while 循环应该有 and 条件。

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

你应该在最后一个 do while 循环中删除那个 invalidgender 条件,否则即使你想按 X 退出它也会导致无限循环。相反,无效的性别条件可以放在主 While 循环的开头。

并且 invalidGender 变量应该再次赋值,否则它将获取之前存储的值。

  invalidGender = ( !(gender == 'm' ||
gender == 'M' ||
gender == 'f' ||
gender == 'F' ));

完整代码

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

关于C++ 循环和 bool 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43175590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com