在这种情况下,为什么我可以使用多种条件与逻辑运算符? C++
#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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在阅读CIN输入3次,
您的问题是,如果您想测试是否有输入错误,则
然后才能进行
!CIN
即your problem is that you are reading cin input 3 times before you do anything
if you want to test if there was an input error just do
!cin
ie
您使用的是
cin&gt; days
的三个顺序调用,或者
只是没有任何意义。
该代码可以以下方式查看:例如:
You are using three sequential calls of
cin>>days
or
That just does not make any sense.
The code can look the following way, for example:
您正在调用
cin&gt;&gt;天
太多次了。您正在提示用户输入,然后在输入循环之前读取
days
,但您完全忽略了用户的初始输入。然后,一旦您进入循环,您就会在做其他任何事情之前再次阅读
days
。但是您还没有提示用户获得另一个输入。最终,希望他们会弄清楚您的程序正在等待另一个输入,并且会键入 inthe in。但是,如果该输入未能读取一个数字,或者数字不超出范围,那么您就是提示用户并在
days中读取
再次跳到下一个循环迭代之前,然后再次启动不良模式,等待额外的输入而不提示用户输入任何输入。您应该在
天阅读
每个循环迭代只有1次。其他人建议如何将输入验证到现有的循环结构中。相反,我建议使用略有不同的循环结构,这应该使代码更容易理解和遵循:
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: