Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

Isdigit() not working for values in range of 48- 58 in C

I am trying to create program which checks if input from user is a number. This works fine for all of the number and charatcers entered, except for numbers in range of 48 to 57. I've been looking through StackOverflow forum and could not find the answer. Could you please advise, what I am doing wrong?

Here is my code:

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

int main(){
    int tab[100];
    int input;
    int index = 0,count=0;
    printf("Podaj liczby:
");
    do{
        scanf("%i", &input);
        if(!isdigit(input)){
            if(input!=0){
                tab[index] = (int)input;
                index++;
            }
        }else{
            printf("Incorrect input");
            return 1;
        }
    }while(input!=0 && index<100);

    if(index<2){
        printf("not enough data available");
        return 2;
    }
    for(int i = 0; i <index; i++){
        count = 0;
        for(int j = i+1;j< index; j++){
            if((tab[i] == tab[j]) && tab[i]!=0) {
                count++;
                tab[j] = 0;
            }
        }
        if(count>0){
            printf("%i ",tab[i]);
        }
    }


    return 0;
}
question from:https://stackoverflow.com/questions/65643145/isdigit-not-working-for-values-in-range-of-48-58-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
   scanf("%i", &input);

scanf with the format %i converts its input to an integer. If the user presses 4 2 Enter, that makes the scanf call equivalent to input = 42.

If the input can't be converted to an integer, scanf returns EOF and doesn't set input. Since you don't check the return value of scanf, your program continues with an indeterminate value in input. (In theory that's undefined behavior, but in practice your program will run with whatever happened to be in the memory location of input.)

   if(!isdigit(input)){

This tests whether the number input is the numerical code of a digit character. In practice, the correspondence between characters and their numerical codes on your computer is ASCII, so digits occupy the range from 48 to 57 inclusive. So the body of this if clause only runs if input is not between 48 and 57.

I have no idea why you'd do that. If you meant to check whether the input is valid, check the return value of scanf. If you thought you were checking the first character of the input, then 1. no you aren't, you're checking the result of the conversion, you can't access the raw input from the user; and 2. you'd only be checking one character anyway so your check couldn't possibly be correct.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...