如何制作cin>>不能将浮点数转换为整数? [英] How to make cin >> not convert float to integer?

查看:164
本文介绍了如何制作cin>>不能将浮点数转换为整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简单代码:

#include <iostream>
int main()
{
   int a;
   std::cout << "enter integer a" << std::endl;
   std::cin >> a ;

   if (std::cin.fail())
   {
      std::cin.clear();
      std::cout << "input is not integer, re-enter please" <<std::endl;
      std::cin >>a;
      std::cout << "a inside if is: " << a <<std::endl;
   }
   std::cout << "a is " << a <<std::endl;
   std::cin.get();
   return 0;
}

运行上面的代码并输入: 1.5 ,它输出: a是1 。仅供参考:我使用gcc 4.5.3。编译并运行代码。

When I run the above code and input: 1.5, it outputs: a is 1. FYI: I compile and run the code with gcc 4.5.3.

这意味着如果 cin 需要一个整数但看到一个浮动,它将隐式进行转换。那么这是否意味着当 cin 看到一个浮点数时,它就不在 fail()状态吗?为什么会这样呢?是因为C ++在>> 运算符上进行隐式转换吗?

This means that if cin expects an integer but sees a float, it will do the conversion implicitly. So does this mean that when cin sees a float number, it is not in fail() state? Why this is the case? Is it because C++ does implicit conversion on >> operator?

我还尝试了以下代码来确定根据这篇文章的想法,给定的输入数字是否为整数:测试给定的数字是否为整数

I also tried the following code to decide whether a given input number is integer following idea from this post: testing if given number is integer:

#include <iostream>
bool integer(float k)
{
    if( k == (int) k) return true;
    return false;
}

int main()
{
   int a;
   std::cout << "enter integer a"<< std::endl;
   std::cin >> a ;

   if (!integer(a))
   {
     std::cout << "input is not integer, re-enter please" ;
     std::cin.clear();
     std::cin >> a;
     std::cout << "a inside if is: " << a <<std::endl;
   }
   std::cout << "a is " << a <<std::endl;
   std::cin.get();
   return 0;
}

此代码块也无法测试 a 是整数,因为当我使用浮点输入运行它时,它只是跳过了 if 块。

This block of code was also not able to test whether a is integer since it simply skip the if block when I run it with float input.

那么在获得用户使用cin的输入时为什么会出现这种情况?如果有时我希望输入为 189 ,但是偶然输入了 18.9 ,该怎么办呢? $ c> 18 在这种情况下,这很糟糕。那么这是否意味着使用 cin 获取用户输入整数不是一个好主意吗?

So why this is the case when getting user input with cin? What if sometimes I want the input to be 189, but typed 18.9 by accident, it will result in 18 in this case, which is bad. So does this mean using cin to get user input integers is not a good idea?

谢谢。

推荐答案

当您读取一个整数并且您给它一个1.5的输入,它看到的是整数1,由于它不是整数的一部分,所以它在句点处停止。 .5仍在输入中。这是您只得到整数部分的原因,也是它似乎不等待第二次输入的原因。

When you read an integer and you give it an input of 1.5, what it sees is the integer 1, and it stops at the period since that isn't part of the integer. The ".5" is still in the input. This is the reason that you only get the integer part and it is also the reason why it doesn't seem to wait for input the second time.

要解决这个问题,您可以读取浮点数而不是整数,以便读取整个值,或者可以在读取整数后检查行上是否还有其他内容。

To get around this, you could read a float instead of an integer so it reads the whole value, or you could check to see if there is anything else remaining on the line after reading the integer.

这篇关于如何制作cin&gt;&gt;不能将浮点数转换为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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