简体   繁体   English

检查输入的值是否为整数

[英]Checking if the entered value is integer

So there is my code 所以有我的代码

void otherdata(int* targetValue, int* count){
    char buffer[256];
    while (1) {
        if (fgets(buffer, sizeof(buffer), stdin)) {
            int lsize = atoi(buffer);
            if (isdigit(lsize) != 0) {
                printf("Target value: %d entered\n", lsize);
                *targetValue = lsize;
                return;
            } else {
            printf("Invalid input. Try again.\n");
            }
        }
    }
}

To me it seems it should work, however it doesn't. 对我来说,它似乎应该起作用,但事实并非如此。 I think the problem is with the isdigit() but I don't another way how to check if it is num. 我认为问题出在isdigit()上,但我没有另一种方法来检查它是否为num。

void otherdata(int* targetValue, int* count){
    char buffer[256];
    while(1) {
    if (fgets(buffer, sizeof(buffer), stdin)) {
        int lsize = atoi(buffer);
        if (isdigit(*buffer) != 0) {             // <<< Changed this line.
            printf("Target value: %d entered\n", lsize);
            *targetValue = lsize;
            return;
        } else {
        printf("Invalid input. Try again.\n");
        }
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM