C++循环和布尔表达式

问题描述:

我在尝试为我的课程做了几天这样的任务时遇到了麻烦,希望得到一些帮助。C++循环和布尔表达式

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

在程序结束时,它希望输出被接受的候选人的数量以及接受候选人总数的平均值。

分配 - https://www.saddleback.edu/faculty/slinker/CS1A/CS1A_Fall2013/Assignment8.pdf

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

我遇到的问题是:

  1. 如果我输入的所有3个是有效的,所以如果他们接受,如果一个不都输出输入(身高或体重)或两者都被拒绝了,那为什么不输出呢。我的布尔变量是不正确的?如果是这样,我该如何解决它。

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

  3. 是否有任何选择陈述我可以变成“不选择陈述”。

这里是我的代码

#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

我有一些坏消息。除了任何错误,您的键盘似乎已损坏。 TAB键不起作用。结果,所显示的代码缺乏合乎逻辑的有意义的缩进,并且大部分是不可读的。你需要修理你的键盘。毕竟,如果您要求其他人帮助解决您的编程问题,至少您可以做的是花费一点努力使您的代码易于阅读,并尽可能使用逻辑缩进。如果你甚至不想正确地缩进你的代码,为什么有人会想要帮助你? –

+0

@SamVarshavchik 现在是否修复?没有意识到你发布时不能使用标签。所以我只做了4个空格。 –

+0

你在哪里声明'invalidGender'?当我运行你的代码时,编译器给了我错误:'|| ===在“no project”中编译文件:“no target”(编译器:未知)=== | ||在函数'int main()'中:|| 31 | error:'invalidGender'未在此范围内声明|| 42 |错误:'invalidGender'未在此范围内声明| || ===构建失败:2个错误,0个警告(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; 
    } 

您应该删除invalidgender条件在过去do while循环,否则会导致无限循环,即使你想退出按十 取而代之的是无效的性别状况可以放置在起点主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; 
} 
+0

嗨Chandini,谢谢你的回应。我在第二次性别输入中再次添加invalidGender变量赋值(最后一次),但我不明白我应该从最后一次做到无效性别条件的时间到主要时间。 –

+0

如果我在上一次删除无效条件的时候,我会添加什么呢?因为在空白的时候我不能离开这个时候的状态。我如何将它添加到主while循环的开始? –

+0

好吧,我这样做,但现在我的节目结束后,我把性别 –