How to check if the input is a valid integer without any other chars?

后端 未结 7 1852
故里飘歌
故里飘歌 2020-11-28 15:36
#include 
#include 

using namespace std;

int main()
{
    int x;
    cout << \"5 + 4 = \";
    while(!(cin >> x)){
               


        
相关标签:
7条回答
  • 2020-11-28 15:52

    If you can use C++11 (and your compiler has full regex support), you can also use the <regex> library:

    #include <iostream>
    #include <limits>
    #include <regex>
    #include <string>
    #include <utility>
    
    int main()
    {
        std::string line;
        std::pair<int, bool> value = std::make_pair(0, false);
        std::cout << "5 + 4 = ";
        while (!value.second)
        {
            while (!std::getline(std::cin, line))
            {
                std::cout << "Error, please try again." << std::endl;
                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            }
    
            if (!std::regex_match(line, std::regex("(\\+|-)?[[:digit:]]+")))
            {
                std::cout << "Error, please try again." << std::endl;
            }
            else
            {
                value = std::make_pair(std::stol(line), true);
            }
        }
    
        if (value.first == (5 + 4))
        {
            std::cout << "Correct!" << std::endl;
        }
        else
        {
            std::cout << "Incorrect!" << std::endl;
        }
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-28 16:06

    You could read a string, extract an integer from it and then make sure there's nothing left:

    std::string line;
    std::cin >> line;
    std::istringstream s(line);
    int x;
    if (!(s >> x)) {
      // Error, not a number
    }
    char c;
    if (s >> c) {
      // Error, there was something past the number
    }
    
    0 讨论(0)
  • 2020-11-28 16:06
    bool isIntegerNumber(const std::string& string){
      std::string::const_iterator it = string.begin();
      int minSize = 0;
      if(string.size()>0 && (string[0] == '-' || string[0] == '+')){
        it++;
        minSize++;
      }
      while (it != string.end() && std::isdigit(*it)) ++it;
      return string.size()>minSize && it == string.end();
    }
    
    0 讨论(0)
  • 2020-11-28 16:08

    try this:

    std::string input;
    std::cin >> input;
    
    if ( std::all_of(input.begin(), input.end(), std::isdigit) )
    {
         //input is integer
    }
    

    Refer this :

    C++ Fix for checking if input is an integer

    0 讨论(0)
  • 2020-11-28 16:09

    For the reason this happens, take a look at this link:

    Extracts and parses characters sequentially from the stream to interpret them as the representation of a value of the proper type, which is stored as the value of val. Internally, the function accesses the input sequence by first constructing a sentry object (with noskipws set to false). Then (if good), it calls num_get::get (using the stream's selected locale) to perform both the extraction and the parsing operations, adjusting the stream's internal state flags accordingly. Finally, it destroys the sentry object before returning.

    Then observe the behavior if you attempt something like this:

    int x = 0;
    
    cin >> x;
    std::cout << x << std::endl;
    std::cout << cin.good() << std::endl;
    
    g++-4.8 -std=c++11 -O3 -Wall -pedantic -pthread main.cpp && echo "900a100" | ./a.out
    // Output:
    // 900
    // 1
    

    If you input "a100" instead, it outputs:

    0
    0
    
    0 讨论(0)
  • 2020-11-28 16:11

    One I have seen that works for some situations is:

    • Read the input as string. cin >> str
    • Decode to number: atoi, or sscanf, or stringstream, etc.
    • print the number into a string (using sprintf or stringstream)
    • check if its equal to read string. (using strings ==, not char*)

    Quick and simple to do. Uses the Cin>>str word breaking rules, accept negative numbers, rejects overflowing numbers. But it does reject "+10", which in somesituations you are happy wiht, and in some you are not.

    0 讨论(0)
提交回复
热议问题