How to check if a string is a number?

后端 未结 10 2539
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 10:03

I want to check if a string is a number with this code. I must check that all the chars in the string are integer, but the while returns always isDigit = 1. I don\'t know wh

10条回答
  •  一整个雨季
    2020-11-29 10:53

    rewrite the whole function as below:

    bool IsValidNumber(char * string)
    {
       for(int i = 0; i < strlen( string ); i ++)
       {
          //ASCII value of 0 = 48, 9 = 57. So if value is outside of numeric range then fail
          //Checking for negative sign "-" could be added: ASCII value 45.
          if (string[i] < 48 || string[i] > 57)
             return FALSE;
       }
    
       return TRUE;
    }
    

提交回复
热议问题