eclipse c++循环和布尔表达式

bf1o4zei  于 2023-02-18  发布在  Eclipse
关注(0)|答案(2)|浏览(202)

我有麻烦尝试做这个作业为我的班级几天了,现在需要一些帮助。
任务是编写一个程序,根据用户的身高和体重资格(取决于他们的性别)通知用户他们的接受状态。
在程序结束时,它希望输出被接受的候选人的数量以及这些被接受的候选人占候选人总数的平均值。
赋值-https://www.saddleback.edu/faculty/slinker/CS1A/CS1A_Fall2013/Assignment8.pdf
我们不能使用开关、条件操作符和选择(仅用于向结果输出正确的消息),只能使用循环和复杂的布尔表达式
我的问题是:
1.如果我的3个输入都是有效的,为什么他们不输出,如果他们被接受,如果其中一个输入(身高或体重)或两者都被拒绝,那么为什么它不输出它。我的布尔变量不正确吗?如果是这样,我如何修复它。
1.为什么当我输入X时无法退出循环/程序。我的while循环是否正确?
1.是否有任何选择语句可以更改为“非选择语句”。
下面是我的代码

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

czq61nw11#

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

您应该删除最后一个do while循环中的invalidgender条件,否则即使您希望按X退出,也会导致无限循环。相反,可以将无效的gender条件放在主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;
}
s5a0g9ez

s5a0g9ez2#

布尔数据类型需要声明为真或假,并且需要在迭代之前设置。
还有:
程序需要在用户输入x退出之前运行。
std:字符串退出;
退出="XXX";
然后使用选择结构。

相关问题