c ++循环和布尔表达式

时间:2017-04-03 02:13:01

标签: c++ eclipse

我现在无法为我的班级做好这几天的任务,并希望得到一些帮助。

任务是编写一个程序,根据用户的性别,根据他们的身高和体重资格,告知用户他们的接受状态。

在计划结束时,它希望输出被接受的候选人数和接受的候选人平均数。

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

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

我遇到的问题是:

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

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

  3. 是否有任何选择陈述我可以改为"而不是选择陈述"。

  4. 这是我的代码

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

1 个答案:

答案 0 :(得分:1)

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

你应该删除最后一次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;
}