cin为什么将我的双输入转换为整数? C ++ [英] Why does it look like that cin converts my double input into integer? C++

查看:135
本文介绍了cin为什么将我的双输入转换为整数? C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

//...
int variable_1, variable_2;
cout << "Please enter the 2 numbers: ";

try
{   
    if ( !(cin >> variable_1) ) throw Invalid_number(); //empty class just for exceptions
    if ( !(cin >> variable_2) ) throw Invalid_number(); //problem is here.
}
catch (Invalid_number) {cerr << "You must enter integers\n"; return -1;}
//...

因此,如果我为变量_1和变量_2的两倍,程序不会引发异常,它只会继续执行程序的其余部分。通过调试,我发现当我输入一个double变量_2(即2.4)时,它只是将其转换为2,并且不会引发异常。相反,如果我为变量_1输入一个双精度数,它将引发异常,并被捕获,程序结束(返回-1)。有谁知道为什么会这样,以及如何使每次输入的两个数字中的任何一个都不是整数时都引发异常?

So if I enter an integer for variable_1 and a double for variable_2, the programme doesn't throw the exception, it just continues executing the rest of the programme. From debugging I saw that when I enter a double variable_2 i.e 2.4 it just converts it to 2 and it doesn't throw an exception. Instead if I enter a double for variable_1, it throws the exception, it is caught and the programme ends (return -1). Does anyone know why this happens and how to make it to throw the exception everytime any of the two numbers entered is not an int?

推荐答案

当您使用 std :: istream 读取 int 时,它会先跳过前导空格,然后尝试读取符号(如果没有则假定为正),然后是数字序列。只要没有更多数字,它就会停止读取。如果可以读取至少一位数字,并且该数字序列与符号组合产生该值,则读取成功。否则,读取将失败。

When you read an int using an std::istream it first skips leading space, then tries to read a sign (it assumes positive if there is none), followed by a sequence of digits. It stops reading as soon as there are no more digits. The read succeeds if at least one digit could be read and the sequence of digits combined with the sign yields the value. Otherwise the read fails.

在您的第一种情况下,将完全读取第一个值,然后读取而保留小数点及其后的所有内容均未读取。但是,读取都可以成功。

In your first case the first value is read completely followed by a read leaving the decimal point . and anything following it unread. However, the reads are both successful.

在第二种情况下,将读取小数点前的数字,而将小数点保留在流中。第二次读取尝试读取 int 并没有找到小数点。

In you second case the digits before the decimal point are read leaving the decimal point in the stream. The second read attempts to read an int and fails finding the decimal point.

如果存在则使读取失败其他的事情有些棘手,因为不清楚什么输入会失败!您可以在读取后检查下一个字符是否为预期字符,例如空格。 ...或者您可以阅读该行的其余部分并验证它是否仅包含空格:这是在少数情况下正确进行格式化和未格式化之间的切换的一种情况。

Making the read fail if there is something else is a bit tricky because it is unclear what inputs should fail! You could check after the read that the next character is an expected character, e.g., a whitespace. ... or you could read the remainder of the line and verify that it only contains whitespaces: that would be one of the rare cases where switch between formatted and unformatted without any reads between is the right thing to do.

通过覆盖 std :: num_get< ...>中相应的 do_get()成员方面,并安装 std :: locale 对象,该对象在发现以下预期失败情况之一时会失败,即读取 int 可能会失败。我怀疑这比这里的合理情况要复杂一些。

By overriding the corresponding do_get() member of the std::num_get<...> facet and installing a std::locale object which fails upon finding one of the expected failure cases the reading of an int could be made to fail. I suspect that is a bit more complex than is being reasonable over here, though.

这篇关于cin为什么将我的双输入转换为整数? C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆