为什么cin将一个浮点值分成两部分? [英] Why will cin split a floating value into two parts?

查看:212
本文介绍了为什么cin将一个浮点值分成两部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对cin有问题。

  int main(void)
{
int a ;
float b;
cin>> a>> b;
}



当我给出一个浮动数字(例如3.14)作为输入时, code> a 也不是 b 获得完整值(3.14):输出是a = 3,b = 0.14。 >

我知道 cin 会将输入按空格,制表符或Return分割,但是'dot'不会,对吗?



以下代码为什么会工作?

  void)
{
int i = 0;
int k = 0;
float j = 0;

cin>> i>> k>> j; // i = 3,j = k = 0
}

编译器会为我们做什么好处?



谢谢!

解决方案

格式化的输入函数工作很简单:


  1. 如果有空格,它们会跳过前导空格。


  2. 如果读取值失败,因为数据与所需的格式不匹配,则会设置 std :: ios_base: :failbit 。如果读取失败,输入不应更改尝试读取的变量(标准输入操作符遵循此规则,但用户定义的输入操作符可能不会)。

您尝试读取的第一个值是 int 。读取 int 意味着读取可选的前导符号后跟一个数字序列(其中,根据您的设置和给定的值,流可能读取八进制或十六进制数字而不是小数)。也就是说, int 接收到 3 的值,并在前读取停止。



根据您接下来阅读的内容,下一次读取失败或不会:




  • 在第一个代码中,您尝试读取以可选符号开头的浮点值,后跟可选的整数部分,后跟可选的千位分隔符,后跟可选的小数部分,后跟可选指数。在积分或分数部分中至少需要一位数字。

  • 在尝试读取整数时,是一个小数部分,发现其不是 int 的有效部分并且读取失败。



尝试读取值后,应始终尝试读取操作是否成功并报告潜在错误:

  if >> value){
std :: cout< 成功读取<值<< '\\\
;
}
else {
std :: cerr<< 无法从input\\\
读取值;
}

请注意,读取失败后您可能还需要清理,例如,使用

  in.clear(); 
in.ignore();

这首先清除错误标志(没有这个,流会忽略任何进一步尝试读取数据),然后忽略下一个字符。


I have a problem about cin.

int main(void)
{
    int a;
    float b;
    cin >> a >> b;
}

When I give a floating number (such as 3.14) as input, neither a nor b get the complete value (3.14): the output is a=3, b=0.14.

I know that cin will split the input by space, tab or Return, but 'dot' will not, right?

And why will the following code work?

int main(void)
{
    int i=0;
    int k=0;
    float j=0;

    cin >> i >> k >> j;    // i =3, j=k=0
}

And one more problem, what benefit will compiler do this for us?

Thanks!

解决方案

The formatted input functions work quite simple:

  1. They skip leading whitespace if any.
  2. They try to read a format matching the type given.
  3. If reading the value fails because the data doesn't match the required format, they set std::ios_base::failbit. If reading fails the input shouldn't change the variable attempted to be read (the standard input operators follow this rule but user defined input operator might not).

The first value you try to read is an int. Reading an int means that a optional leading sign is read followed by a sequence of digits (where, depending on your settings and the value given, the stream may read octal or hexadecimal numbers rather than decimal ones). That is, the int receives the value 3 and reading stops right in front of the ..

Depending on what you read next, the next read fails or doesn't:

  • In the first code you try to read a floating point value which starts with an optional sign, followed by an optional integral parts, followed by an optional thousands separator, followed by an optional fractional part, followed by an optional exponent. At least one digit is required in either the integral or the fractional part. In your example, there is only a thousands separate followed by a fractional part.
  • When trying to read an integer, a . is found which isn't a valid part of an int and reading fails.

After attempting to read a value, you should always try if the read operation was successful and report potential errors:

if (in >> value) {
    std::cout << "successfully read '" << value << "'\n";
}
else {
    std::cerr << "failed to read a value from input\n";
}

Note, that after a failed read you may need to clean up as well, e.g., using

in.clear();
in.ignore();

This first clears the error flags (without this, the stream would ignore any further attempts to read the data) and then it ignores the next character.

这篇关于为什么cin将一个浮点值分成两部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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