简体   繁体   English

如何检查输入的数字整数是否不是浮点数?

[英]How to check if the input number integer not float?

I want to check if the input is valid, but when i do run this code I see that it checks only input for charcters.我想检查输入是否有效,但是当我运行此代码时,我看到它只检查字符输入。 If i input a float number it will take it and going to use like integer without fractional part.如果我输入一个浮点数,它将接受它并使用没有小数部分的整数。

#inclide <iostream>
using namespace std;
...
int n;
cout << "Your input is: "<<endl;
cin >> n;
while (cin.fail()) {
    cout << "Error. Number of elements must be integer. Try again: " << endl;
    cin.clear();
    cin.ignore(256, '\n');  
    cin >> n;
}
...        
      `

So, how to make this code see if the input is float?那么,如何让这段代码查看输入是否为浮点数呢?

You can try to convert the input string to a int using a std::istringstream .您可以尝试使用std::istringstream将输入字符串转换为int If it succeeds then check for eof() (after ignoring blank spaces) to see if the whole input was consumed while converting to int .如果成功,则检查eof() (在忽略空格之后)以查看在转换为int是否消耗了整个输入。 If the whole input was consumed then it was a valid int .如果整个输入都被消耗了,那么它是一个有效的int

Something a bit like this:有点像这样:

int input_int()
{
    int i;

   // get the input
    for(std::string line; std::getline(std::cin, line);)
    {
        // try to convert the input to an int
        // if at eof() all of the input was converted - must be an int
        if(!line.empty() && (std::istringstream(line) >> i >> std::ws).eof())
            break;

        // try again
        std::cout << "Not an integer please try again: " << std::flush;
    }

    return i;
}

int main()
{
    std::cout << "Enter an integer: " << std::flush;

    std::cout << "i: " << input_int() << '\n';
}

Building on Raindrop7 's solution, here's the full code to do what you need:Raindrop7的解决方案为基础,以下是完成您需要的完整代码:

#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    double m;
    cout << "Your input is: "<<endl;
    cin >> m;
    while (cin.fail() || (m-floor(m)))
    {
        cout << "Error. Nubmer of elements has to be integer. Try again: " << endl;
        cin.clear();
        cin.ignore(256, '\n');  
        cin >> m;
    }
    int n = (int)m;
    return 0;
}

Here's a sample output:这是一个示例输出:

Your input is: 
2.7
Error. Nubmer of elements has to be integer. Try again: 
erer
Error. Nubmer of elements has to be integer. Try again: 
2

The code below should be able to do what you are hoping to achieve:下面的代码应该能够完成您希望实现的目标:

#inclide <iostream>
using namespace std;
int n;
cout << "Your input is: "<<endl;
while (!(cin >> n) || cin.get() != '\n') {
    cout << "Error. Number of elements must be integer. Try again: " << endl;
    cin.clear();
    cin.ignore(256, '\n');  
}

The program asks the user to re-enter an integer if either of the following happens:如果发生以下任一情况,程序会要求用户重新输入整数:

  1. If the program is unable to extract an integer from the std::cin stream.如果程序无法从std::cin流中提取整数。 (For example, when a character or string is entered by the user) (例如,当用户输入字符或字符串时)
  2. If, after an integer is extracted successfully, the next character in std::cin is not the new line '\\n' character.如果在成功提取整数后, std::cin的下一个字符不是新行'\\n'字符。 (For example, when a number with a decimal point like 1.1 is entered, or when an integer followed by a character like 1a is entered.) (例如,输入带小数点的数字1.1或输入的整数后跟字符1a 。)

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

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