I'm working on a program in C in which it will request a response, then use the first appearance of a digit (using isdigit() ) and then assign it to a variable for later use. After testing a bit I realized it was taking the ascii value for 10, which is line feed. Here is the code snippet.
while(isdigit(ch) == 0){
ch = getchar();
}
ch = getchar();
star = ch;//value is being detected as 10, which is line feed.
I tried casting ch to int when assigning it to star, which did not change the resulting value of 10. Is my issue within the assignment of ch, or is it poor syntax?
YOur problem is that you see a digit, then you read the next character
while(isdigit(ch) == 0){
ch = getchar();
}
ch = getchar(); <<<======
Just use the value in ch that you have already, plus you need to convert '0' to 0, not the same thing
while(isdigit(ch) == 0){
ch = getchar();
}
int inp = ch - '0';