如何使用do while循环中的sentinel值编写while循环?

时间:2015-04-20 00:07:59

标签: c++ loops for-loop break

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

1 个答案:

答案 0 :(得分:0)

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中的商店值)
  3. 首先我们需要正确设置变量:

    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,以便我们避免整数除法,这会导致我们丢失小数位并仅返回整数。