gpt4 book ai didi

c++ - 如何使用 do while 循环中的标记值编写 while 循环?

转载 作者:太空宇宙 更新时间:2023-11-04 13:35:24 25 4
gpt4 key购买 nike

我在为这段将性别作为循环控制变量的代码编写 while 循环时遇到问题(当您输入性别时,它处于一个 do-while 循环中,具有特定的条件才能使其有效)。另外,我不知道如何才能计算出被录取的候选人的数量和百分比。

     char gender;     //INPUT       Gender of candidate
char genderOk; //CALC & OUT Checks validity of gender input
int height; //INPUT Height of candidate
int heightOk; //CALC & OUT Valid height range for candidate
int weight; //INPUT Weight of candidate
int weightOk; //CALC & OUT Valid weight range for candidates
int count; //INPUT Counter of the FOR loop
int candidates; //INPUT Amount of candidates per test
int acceptCand; //CALC & OUT # of candidates accepted per test
int acceptPerc; //CALC & OUT % of candidates accepted per test


// INPUT - describe input here

cout << left;

for(count = 1; count <= 3; count++)
{
cout << "TEST RUN #" << count << endl << endl << endl;

cout << "Please enter candidate's information (enter 'X' to "
"exit).\n";

gender = 'f';

while(gender != 'X' || gender != 'x')
{
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!\n\n\n\n";
}
else
{
if(!heightOk)
{
if(!weightOk)
{
cout << RESULT << "rejected based on the "
"HEIGHT and WEIGHT "
"requirements."
"\n\n\n\n";
}
else
{
cout << RESULT << "rejected based on the "
"HEIGHT requirement."
"\n\n\n\n";
}
}
else
{
if(!weightOk)
{
cout << RESULT << "rejected based on the "
"WEIGHT requirement."
"\n\n\n\n";
}
}
}
}

if(gender == 'f' || gender == 'F')
{
heightOk = height >= 62 && height <=75;
weightOk = weight >= 110 && weight <= 185;

if(heightOk && weightOk)
{
cout << RESULT << "ACCEPTED!\n\n\n\n";
}
else
{
if(!heightOk)
{
if(!weightOk)
{
cout << RESULT << "rejected based on the "
"HEIGHT and WEIGHT "
"requirements.\n\n\n\n";
}
else
{
cout << RESULT << "rejected based on the"
" HEIGHT requirement."
"\n\n\n\n";
}
}
else
{
if(!weightOk)
{
cout << RESULT << "rejected based on the "
"WEIGHT requirement."
"\n\n\n\n";
}
}
}
}
}
}

最佳答案

while循环条件

要解决具有 while 循环条件的问题,而不是检查主循环中输入的性别是什么(这实际上从未被验证,因为性别是在 while 循环中分配的),您的 while 循环的控制条件可以是 bool 变量。

这个:

bool isQuit = false;

while(!isQuit)
{
...

代替:

gender = 'f';

while(gender != 'X' || gender != 'x')
{
...

然后当用户可以输入性别时,您检查退出条件。 (在这种情况下,gender == 'X' || gender == 'x'。)

if(gender == 'X' || gender == 'x') {
isQuit = true;
break;
} else if(genderOk) {
cout << "***** Invalid gender; please enter M or F "
"*****";
cout << endl;
}
...

这将跳出用于输入性别的 do..while 循环。需要添加的只是检查用户是否已选择退出输入循环。

// After the do..while loop for inputting gender
if(isQuit) {
break;
}

现在程序将跳出我们定义为具有 while(!isQuit) 循环条件的主 while 循环。


注意:上面的代码不会跳出运行三个“测试运行”的 for 循环。要使用 isQuit 变量退出整个程序,请在 for 循环末尾添加以下代码:

    if(isQuit) {
break;
}
} // END OF YOUR FOR LOOP

计算候选人

要计算候选人数、已接受候选人数和已接受候选人百分比,我们需要跟踪两件事。

  1. 候选人总数(在candidates中存储值)
  2. 总接受候选人(在acceptCand中存储值)

首先我们需要正确设置我们的变量:

int    candidates; //INPUT       Amount of candidates per test
int acceptCand; //CALC & OUT # of candidates accepted per test
float acceptPerc; //CALC & OUT % of candidates accepted per test

candidates = 0;
acceptCand = 0;
acceptPerc = 0;

每当我们到达用户未输入值的地步时,我们就假设他们输入了有效候选值。在检查候选人性别的地方,我们可以将候选人计数器加 1。

if(gender == 'm' || gender == 'M') {
candidates++;
...

if(gender == 'f' || gender == 'F') {
candidates++;
...

既然我们正在跟踪候选人总数,我们现在需要知道已接受候选人的数量。为此,每次接受候选人时,我们都会将 acceptCand 变量增加一。

if(heightOk && weightOk) {
cout << "ACCEPTED!\n\n\n\n";
acceptCand++;
}

最后,要获得已接受候选人的百分比,您需要执行以下操作:

acceptPerc = (acceptCand / (float)candidates) * 100;

注意:acceptPercfloat 数据类型,因此我们可以使用小数位。我们将 candidates 变量类型转换为 float,这样我们就避免了整数除法,这会导致我们丢失小数位并只返回整数。

关于c++ - 如何使用 do while 循环中的标记值编写 while 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29737378/

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