在这种情况下,为什么我可以使用多种条件与逻辑运算符? C++

发布于 2025-02-06 07:21:26 字数 880 浏览 3 评论 0原文

#include <iostream>

int main(int, char **)
{
   using namespace std;

   int days;

   cout<<"How many days did you work this month? ";
   cin>>days;
   while(!(cin>>days) || (days<1) || (days>31)){
      if(!(cin>>days)){
         cout<<"Number of days should be... well, a number. Please re-enter: ";
         cin.clear();
         cin.ignore(100,'\n');
      }
      else if(days<1 || days>31){
         cout<<"Number of days should be between 1 and 31. Please re-enter:";
         cin>>days;
      }
   }
   return 0;
}

这应该可以验证用户输入:

  • 当您要求INT并进入Char/String时,第一个条件是为了避免无限循环。该部分工作正常,它一直要求输入直到我输入一个数字,然后继续执行程序的下一部分(此处未显示)。

  • 天数应在1到30之间。如果我输入一个数字 - 任何数字在范围内或外部 - 该程序将根本不会做任何事情。甚至不会显示错误消息。

如果我使用两个单独的循环,我可以使它起作用。 (天&gt; 31)。我不知道我在这里做错了什么。

#include <iostream>

int main(int, char **)
{
   using namespace std;

   int days;

   cout<<"How many days did you work this month? ";
   cin>>days;
   while(!(cin>>days) || (days<1) || (days>31)){
      if(!(cin>>days)){
         cout<<"Number of days should be... well, a number. Please re-enter: ";
         cin.clear();
         cin.ignore(100,'\n');
      }
      else if(days<1 || days>31){
         cout<<"Number of days should be between 1 and 31. Please re-enter:";
         cin>>days;
      }
   }
   return 0;
}

This is supposed to validate user input:

  • The first condition is there to avoid an infinite loop when you ask for an int and the user enters a char/string. This part works just fine, it keeps asking for input until I enter a number, and then proceeds to execute the next part of the program (not shown here).

  • the number of days should be between 1 and 30. If I enter a number - any number, inside or outside the range - the program will not do anything AT ALL. Won't even show the error message.

I can make it work if I use two separate loops, one to validate !(cin>>days) and another for (days<1) || (days>31). I don't know what it is that I'm doing wrong here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妥活 2025-02-13 07:21:27

您正在阅读CIN输入3次,

   int days;

   cout<<"How many days did you work this month? ";
   cin>>days;   <<<<<==== 1
   while(!(cin>>days) <<<<<<<=====2|| (days<1) || (days>31)){
      if(!(cin>>days)){  <<<<<<=== 3
         cout<<"Number of days should be... well, a number. Please re-enter: ";
         cin.clear();
         cin.ignore(100,'\n');

      }
      else if(days<1 || days>31){
         cout<<"Number of days should be between 1 and 31. Please re-enter:";
         cin>>days;
      }
   }

您的问题是,如果您想测试是否有输入错误,则

然后才能进行!CIN

   int days;

   cout<<"How many days did you work this month? ";
   cin>>days;
   while(!(cin) || (days<1) || (days>31)){
      if(!(cin)){
         cout<<"Number of days should be... well, a number. Please re-enter: ";
         cin.clear();
         cin.ignore(100,'\n');
         cin >> days; 
      }
      else if(days<1 || days>31){
         cout<<"Number of days should be between 1 and 31. Please re-enter:";
         cin>>days;
      }
   }

your problem is that you are reading cin input 3 times before you do anything

   int days;

   cout<<"How many days did you work this month? ";
   cin>>days;   <<<<<==== 1
   while(!(cin>>days) <<<<<<<=====2|| (days<1) || (days>31)){
      if(!(cin>>days)){  <<<<<<=== 3
         cout<<"Number of days should be... well, a number. Please re-enter: ";
         cin.clear();
         cin.ignore(100,'\n');

      }
      else if(days<1 || days>31){
         cout<<"Number of days should be between 1 and 31. Please re-enter:";
         cin>>days;
      }
   }

if you want to test if there was an input error just do !cin

ie

   int days;

   cout<<"How many days did you work this month? ";
   cin>>days;
   while(!(cin) || (days<1) || (days>31)){
      if(!(cin)){
         cout<<"Number of days should be... well, a number. Please re-enter: ";
         cin.clear();
         cin.ignore(100,'\n');
         cin >> days; 
      }
      else if(days<1 || days>31){
         cout<<"Number of days should be between 1 and 31. Please re-enter:";
         cin>>days;
      }
   }
她如夕阳 2025-02-13 07:21:27

您使用的是cin&gt; days的三个顺序调用

cin>>days;
while(!(cin>>days) || (days<1) || (days>31)){
   if(!(cin>>days)){
//...

,或者

while(!(cin>>days) || (days<1) || (days>31)){
   if(!(cin>>days)){
       //...
   }
   else if(days<1 || days>31){
      //...
      cin>>days;
   }
}

只是没有任何意义。

该代码可以以下方式查看:例如:

int days;
bool failure;

cout<<"How many days did you work this month? ";

while( ( failure = !(cin>>days) ) || (days<1) || (days>31)){
   if( failure ){
      cout<<"Number of days should be... well, a number. Please re-enter: ";
      cin.clear();
      cin.ignore(100,'\n');
   }
   else if(days<1 || days>31){
      cout<<"Number of days should be between 1 and 31. Please re-enter:";
   }
}

You are using three sequential calls of cin>>days

cin>>days;
while(!(cin>>days) || (days<1) || (days>31)){
   if(!(cin>>days)){
//...

or

while(!(cin>>days) || (days<1) || (days>31)){
   if(!(cin>>days)){
       //...
   }
   else if(days<1 || days>31){
      //...
      cin>>days;
   }
}

That just does not make any sense.

The code can look the following way, for example:

int days;
bool failure;

cout<<"How many days did you work this month? ";

while( ( failure = !(cin>>days) ) || (days<1) || (days>31)){
   if( failure ){
      cout<<"Number of days should be... well, a number. Please re-enter: ";
      cin.clear();
      cin.ignore(100,'\n');
   }
   else if(days<1 || days>31){
      cout<<"Number of days should be between 1 and 31. Please re-enter:";
   }
}
孤独难免 2025-02-13 07:21:27

您正在调用cin&gt;&gt;天太多次了。

您正在提示用户输入,然后在输入循环之前读取days,但您完全忽略了用户的初始输入。

然后,一旦您进入循环,您就会在做其他任何事情之前再次阅读days 。但是您还没有提示用户获得另一个输入。最终,希望他们会弄清楚您的程序正在等待另一个输入,并且会键入 inthe in。

但是,如果该输入未能读取一个数字,或者数字不超出范围,那么您就是提示用户并在days中读取 再次跳到下一个循环迭代之前,然后再次启动不良模式,等待额外的输入而不提示用户输入任何输入。

您应该在天阅读每个循环迭代只有1次。

其他人建议如何将输入验证到现有的循环结构中。相反,我建议使用略有不同的循环结构,这应该使代码更容易理解和遵循:

#include <iostream>
#include <limits>

int main()
{
   using namespace std;

   int days;

   cout << "How many days did you work this month? ";
   do {
      if (!(cin >> days)) {
         cout << "Number of days should be... well, a number. Please re-enter: ";
         cin.clear();
         cin.ignore(numeric_limits<streamsize>::max(), '\n');
      }
      else if ((days < 1) || (days > 31)) {
         cout << "Number of days should be between 1 and 31. Please re-enter:";
      }
      else {
         break;
      }
   }
   while (true);

   cout << "Thank you for working " << days << "day(s)!";
   return 0;
}

You are calling cin >> days too many times.

You are prompting the user for input and then reading in days before entering the loop, but you are completely ignoring the user's initial input.

Then, once you are inside the loop, you read in days again before doing anything else. But you didn't prompt the user for another input yet. Eventually, hopefully they will figure out that your program is waiting for another input, and will type something in.

But if that input fails to read a number, or the number is out of range, then you are prompting the user and reading in days yet again before jumping to the next loop iteration, which then starts the bad pattern all over again, waiting for an extra input without prompting the user to enter any input.

You should be reading in days only 1 time per loop iteration.

Other people have suggested how to pigeon-hole the input validation into your existing loop structure. I would instead suggest a slightly different loop structure that should make the code a little easier to understand and follow:

#include <iostream>
#include <limits>

int main()
{
   using namespace std;

   int days;

   cout << "How many days did you work this month? ";
   do {
      if (!(cin >> days)) {
         cout << "Number of days should be... well, a number. Please re-enter: ";
         cin.clear();
         cin.ignore(numeric_limits<streamsize>::max(), '\n');
      }
      else if ((days < 1) || (days > 31)) {
         cout << "Number of days should be between 1 and 31. Please re-enter:";
      }
      else {
         break;
      }
   }
   while (true);

   cout << "Thank you for working " << days << "day(s)!";
   return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文