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

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

using namespace std;

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


        
7条回答
  •  一个人的身影
    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

提交回复
热议问题