I am working through some C videos and I am writing code. I have code below and I don't understand what I am doing incorrectly. ISDigit only returns a 0 no matter whether you enter a digit 0 - 9 or J or some special character so the condition always evaluates to false and the true statement is never executed.
The code is very simple.
It asks for a number between 1 - 255.
(Question 1: I attempted an unsigned char for this and it would not work. Is there no such thing as an unsigned char in C or C++)
It evaluates to input to ensure that it is a number.
(The isdigit)
It checks to ensure that number is between 1 - 255 otherwise it either asks the question again or just exits the program.
If a valid entry was put in. It displays a count down starting with the number entered.
Question 2: Can someone explain to me why this is and offer a solution that would rectify this issue?
The code was saved as a .cpp file but the coding is actually C. I am using Dev C++ Compiler on a Windows 7 64 bit Operating System.
Code:#include <conio.h> #include <iostream> #include <ctype.h> using namespace std; int main(int argc, char *argv[]) { int loopCounter=0,count=0,a; for(a=4;a>0;a--) { printf("Please enter a number 1 - 255: "); scanf("%d",&count); if(isdigit(count)) { if(count<=0) a=2; else if(count>0 && count<256) a=0; else return 1; } else return 1; } while(count>=loopCounter) { printf("%d\n",count); count--; } getch(); return EXIT_SUCCESS; }