简体   繁体   English

关于cin,字符串和大量数字-C ++

[英]About cin, strings, and huge numbers - C++

I've got this very simple code. 我有这个非常简单的代码。 Just an if statement with three outcomes between 1, 2, and everything else. 只是一个if语句,其结果介于1、2和其他所有值之间。 The desired behavior is that after outputting the appropriate response, the program will wait for the user to hit the enter key before closing. 期望的行为是,在输出适当的响应之后,程序将在关闭之前等待用户按下Enter键。 But if a string is entered, cin seems to fail despite the fact it doesn't fail for a single character. 但是,如果输入了一个字符串,则cin似乎会失败,尽管事实对于单个字符也不会失败。 Why is that? 这是为什么?

Also, if I enter a 30 digit number, the program will output "nope" as it should, but the user will need to hit enter twice before it closes. 另外,如果我输入30位数字,程序将按原样输出“ nope”,但用户需要在关闭前按两次Enter键。 How come? 怎么会?

Also, I'm not exactly looking for a solution to the problem, just an understanding as to why these things happen. 另外,我并不是在寻找问题的解决方案,而只是在理解为什么会发生这些事情。

#include <iostream>

int main()
{
int choice;

std::cout<< "1 or 2?"<< std::endl;
std::cin>> choice;
if (choice == 1)
{
    std::cout<< "boom"<< std::endl;
}
else if (choice ==2)
{
    std::cout<< "bam"<< std::endl;
}
else
{
    if (std::cin.fail())
    {
        std::cin.clear();
        std::cin.ignore();
        std::cout<< "nope"<< std::endl;
    }
    else if (choice != 1 || 2)
    {
        std::cout<< "nope"<< std::endl;
    }
}
std::cin.ignore();
std::cin.get();
return 0;
}

Thank you for your time and I apologize if this question is overly simple. 谢谢您的宝贵时间,对于这个问题过于简单,我们深表歉意。 I just had no idea how to phrase it for Google. 我只是不知道如何为Google措辞。

else if (choice != 1 || 2)

This line of code is not doing what you suspect 这行代码没有做您怀疑的事情

You have to actually write the code below 您必须实际编写以下代码

else if (choice != 1 || choice != 2)

This is subtle because of the way we speak English. 这是微妙的,因为我们说英语的方式。 Your code would say "else if choice is not equal to 1 or 2... Seems to make logical sense, but if you really think about it...what are you comparing two with. We know we are comparing one with choice, but what about two? 您的代码会说:“否则,如果选择不等于1或2 ...似乎合乎逻辑,但是如果您真的考虑过……您将两者与什么进行比较。我们知道我们正在将一项与选择进行比较,但是两个呢?

The code I showed says more accurately and precisely, "else if choice is not equal to one or choice is not equal to two" Now we know that on both sides of the or, we are comparing the integer values of 1 and 2 with choice 我显示的代码更准确,更准确地说:“否则,如果选择不等于一或选择不等于二”现在我们知道在or的两边,我们正在将1和2的整数值与选择进行比较

NOTE: The else if here is actually entirely useless because you know from the original if and else if and else that the choice is not a one or two. 注意: else if在这里实际上是完全没有用的,因为您从原始的if and else if and else知道选择不是一两个。 The fact that you are in that else block already tells you that choice is not a one or two, So you really don't need this redundancy of logic here. 您在else块中的事实已经告诉您选择不是一两个,因此您在这里实际上不需要这种逻辑上的冗余。

i always used #include <conio.h> 我一直使用#include <conio.h>

and right before return 0 , i do 就在return 0之前

_getch();

also in ur code where u have else if (choice !=1|| 2) , it should be 同样在您的代码中, else if (choice !=1|| 2)还有else if (choice !=1|| 2) ,也应该

else if (choice !=1 || choice !=2)

and the reason why you are having to hit enter twice before it closes when you enter any number larger thatn 10 digits is because you have an extra 而当您输入大于10位数字的数字时,必须在输入关闭之前按回车两次的原因是,因为您有一个额外的

std::cin.ignore(); 的std :: cin.ignore();

after the last else statement. 在最后的else语句之后。

so basically when it goes past the 10th digit it will ignore what ever is next and then wait for the user to hit enter (or any key) again. 因此基本上,当它超过第10个数字时,它将忽略下一个数字,然后等待用户再次按Enter(或任何键)。 then it will close out. 然后它将关闭。

I could be wrong.. but I think thats the reason why.. 我可能是错的..但我认为那是原因..

int has a limit on how big it can get.. int对其大小有限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM