Go Down

Topic: isdigit function problem (Read 3967 times) previous topic - next topic

rehakmatej

Hello, I have a program, where isdigit() function is used to check, if the character in char array is number. But in my program this function doesn't work properly. Here is my program.
Code: [Select]
if (isdigit(getRequest[5]) == 1)
{
room_num = getRequest[5] - '0';
Serial.print("room_num = ");
Serial.println(room_num, DEC);
Serial.print("getRequest[8] = ");
Serial.println(getRequest[8] - '0', DEC);
if (getRequest[7] == 't')
{
if (isdigit(getRequest[8] == 1))          //here it returns 0, but should return 1
{
Serial.println("getRequest[0] is DIGIT");
temp_num = getRequest[8] - '0';
Serial.print("temp_num = ");
Serial.println(temp_num, DEC);
}
                 }

Thank you for help.

rehakmatej

I solved it  :)  if (isdigit(getRequest[8] == 1)) should be if (isdigit(getRequest[8])==1)  Bad backets

MarkT

Both are arguably wrong, you should not assume that isDigit() returns 1 for true, you can
only assume the result is 0 for false.  Just use:

Code: [Select]

  if (isDigit (getRequest [8]))


Many things in C do pass 1 for true, but everything that _uses_ a boolean value(*) only
assumes false is represented by a zero value, so do likewise.

(*) if, while, !, #if
[ I DO NOT respond to personal messages, I WILL delete them unread, use the forum please ]

Go Up