【问题标题】:How to write a while loop using a sentinel value that's inside a do while loop?如何使用 do while 循环中的标记值编写 while 循环?
【发布时间】:2015-04-20 00:07:59
【问题描述】:

我在为这个将性别作为循环控制变量的代码编写 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";
                    }
                }
             }
         }
       }
     }

【问题讨论】:

  • 什么是“LCV”?你的具体问题是什么?如果您在终止循环时遇到问题,请发布显示特定循环的特定问题的代码(最好是最少的代码)。
  • @Michael - 我认为 Brothermilk 是编程新手,它可能来自一本书(我在那里找到了对 lcv 和哨兵的引用)。我删除了它,因为它对大多数 C/C++/Java/... 人来说并不常见。我希望它不为人所知,而我应该知道它:)
  • 您的性别循环前面是打印的消息“X 退出”,但测试未测试“X”...
  • 我发现“LCV”代表“循环控制变量”。
  • 抱歉不清楚,是的,我是编程新手(但我没有从书中学习“LCV”)。感谢 jonathan leffler 发现我的循环存在问题,但我仍然不知道如何在循环中测试“X”。

标签: c++ loops for-loop break


【解决方案1】:

while 循环条件

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

这个:

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++;
...

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

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

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

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

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

【讨论】:

    猜你喜欢
    • 2013-07-09
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 2016-03-27
    • 2020-08-28
    • 1970-01-01
    相关资源
    最近更新 更多