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

前端 未结 7 505
没有蜡笔的小新
没有蜡笔的小新 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:10

    The input to isdigit is an integer value. However, it will return true (non-zero) only if the value corresponds to '0'-'9'. If you convert them to integer values, they are 48-57. For all other values, isdigit will return false (zero).

    You can check whether you got an integer by changing checking logic:

    if ( cin.fail() )
    {
       cout<<"Correct"<

提交回复
热议问题