Check if the input is a number or string in C++

前端 未结 7 494
没有蜡笔的小新
没有蜡笔的小新 2020-12-10 06:16

I wrote the following code to check whether the input(answer3) is a number or string, if it is not a number it should return \"Enter Numbers Only\" but it returns the same e

相关标签:
7条回答
  • 2020-12-10 07:11

    The function isdigit() is used to test for only digits ( 0,1,...,9)

    use this function to check for numbers

    bool is_number(const std::string& s)
    {
        std::string::const_iterator it = s.begin();
        while (it != s.end() && std::isdigit(*it)) ++it;
        return !s.empty() && it == s.end();
    }
    
    0 讨论(0)
提交回复
热议问题