Home c IsDigit function does not work as I expected

# IsDigit function does not work as I expected

Author

Date

Category

How can I find out if the entered digit symbol is?

`IsDigit `function does not work:

``````# include & lt; stdio.h & gt;
#Include & lt; stdlib.h & gt;
#Include & lt; Time.h & gt;
#Include & lt; unistd.h & gt;
#Include & lt; ctype.h & gt;
Int Main (Int Argc, Char Const * ArgV []) {
SRAND (TIME (NULL));
int score = 0;
While (1) {
int x = atoi (argv [1]) + rand ()% ATOI (argv [2]);
int y = atoi (argv [1]) + radand ()% ATOI (argv [2]);
int OperationSignNumber = 1 + Rand ()% 2;
INT RESULT = 0;
Switch (OperationSignNumber) {
Case 1:
Printf ("+ ------------------ + \ n");
PrintF ("% i +% i =? \ n", x, y);
PrintF ("+ ------------------ + \ n");
result = x + y;
Break;
Case 2:
PrintF ("+ ------------------ + \ n");
PrintF ("% i -% i =? \ n", x, y);
Printf ("+ ------------------ + \ n");
result = x - y;
Break;
}
int userswer = 0;
PrintF ("\ N & GT;");
Scanf ("% I", & amp; UseRSWER);
if (! IsDigit (UseRSwer)) Break; // & lt; & lt; ----
System ("Clear");
Printf ("+ ------------------ + \ n");
PrintF ("| Right! | \ n");
Printf ("+ ------------------ + \ n");
SCORE ++;
} else {
Printf ("+ ------------------ + \ n");
PrintF ("| Left! | \ n");
PrintF ("+ ------------------ + \ n");
}
SLEEP (1);
INT TIMER = 3;
While (Timer) {
System ("Clear");
PrintF ("+ ------------------ + \ n");
PrintF ("| Get Ready!% i ... | \ n", Timer);
PrintF ("+ ------------------ + \ n");
Timer--;
SLEEP (1);
}
System ("Clear");
}
System ("Clear");
Printf ("+ ------------------ + \ n");
Printf ("You Score:% i ... \ n", score);
PrintF ("+ ------------------ + \ n");
Return 0;
}
``````

I start the program: `./ Quick_math 1 3 `. Displays `1 + 1 `. I enter `2 `and the program closes. I tried to compile using `Clang 3.6 `and `GCC-4.8.4 `– the result is the same.

The `function isDigit `defines whether the younger byte of an integer number is the `int `specified as an argument, the character code of the number. That is, she does not what you assume.

You just should replace these two proposals

``````scanf ("% i", & amp; useranswer);
if (! IsDigit (UseRSwer)) Break;
``````

One offer:

``````if (scanf ("% i", & amp; userswer)! = 1) Break;
``````

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.