Input validation of an Integer using atoi()

后端 未结 2 1553
鱼传尺愫
鱼传尺愫 2020-11-30 15:33
#include \"stdafx.h\"
#include 

void main()
{
    char buffer[20];
    int num;

    printf(\"Please enter a number\\n\");
    fgets(buffer, 20, std         


        
2条回答
  •  时光取名叫无心
    2020-11-30 16:37

    There is the isdigit function that can help you check each character:

    #include 
    
    /* ... */
    
    for (i=0; buffer[i]; i++) {
            if (!isdigit(buffer[i])) {
                printf("Bad\n");
                break;
            }
    }   
    

提交回复
热议问题