convert string to integer in c++

前端 未结 3 604
故里飘歌
故里飘歌 2020-12-03 22:57

Hello I know it was asked many times but I hadn\'t found answer to my specific question.

I want to convert only string that contains only decimal numbers:

Fo

3条回答
  •  自闭症患者
    2020-12-03 23:28

    The simplest way that makes error checking optional that I can think of is this:

    char *endptr;
    int x = strtol(str, &endptr, 0);
    int error = (*endptr != '\0');
    

提交回复
热议问题