-
what I'm I doing wrong?
I'm new to C programming. I was under the impression that isxdigit function returns true if int is a hexadecimal digit character. I think there's something wrong with my if statement. it won't add the two numbers. What am I doing wrong?
Thanks.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
int reply;
while(1)
{
puts("\n****************************************** ***");
puts("\n");
puts("\nEnter a menu number selection between 1 and 4, 0 to exit: ");
puts("\n");
puts("\n****************************************** ***");
scanf("%d", &reply);
switch (reply)
{
case 0:
exit(0);
case 1:
{
int a,b,c;
printf ("Two numbers please.\n");
scanf ("%x%x", &a, &b);
if (isxdigit == 1)
{
c = a + b;
printf ("%#x", c);
}
else
printf("hex please");
return 0 ;
break;
}
default:
puts("Between 1 and 4, please!\n");
} /* end of switch */
} /* end of while */
return 0;
}
-
This looks like a very strange program to me. Some comments and recommendations.
1. Don't do that exit(0) in the case. If you just put a break in that
case, it will also finish the switch structure.
2. If isxdigit is a function, doesn't it need to have some input
variable(s)?
3. Don't do that return 0 when case 1, a break is all you need.
4. You need a number between 1 and 4? Then you will also
need cases for that.
5. I would use an if-structure for this problem.
6. Why use while(1), this is an endless loop.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
int reply;
puts("\n*********************************************");
puts("\n");
puts("\nEnter a menu number selection between 1 and 4, 0 to exit: ");
puts("\n");
puts("\n*********************************************");
scanf("%d", &reply);
if (reply > 0 && reply < 5)
{
int a, b, c;
printf ("Two numbers please.\n");
scanf ("%x%x", &a, &b);
if (isxdigit (a) && isxdigit (b))
{
c = a + b;
printf ("%X", c);
}
else
{
printf ("Hex please\n");
}
}
else
{
printf ("Nr. between 1 and 4 please\n");
}
return 0;
}
Note that I'm not sure about isxdigit-function. Never used that.
-
> scanf ("%x%x", &a, &b);
> if (isxdigit (a) && isxdigit (b))
isxdigit works on characters, not integers
scanf ("%c%c", &a, &b);
if (isxdigit (a) && isxdigit (b))
-
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
int i;
for (i = 0; i < 255; i++)
{
if (isxdigit (i))
printf ("%X is hexadecimal (%c)\n", i, (char) i);
else
printf ("%X is not hexadecimal (%c)\n", i, (char) i);
}
return 0;
}
Function isxdigit seems to work on integers also. But I guess it's input is only valid if 0 <= i <= 255.
-
I tried using ch, but then I got errors saying that the variables a, b, c were not declared. scanf ("%x%x", &a, &b);
c = a + b;
scanf ("%c%c", &a, &b); did not work at all. I must be still doing something wrong
-
It seems to me that you're trying to do two different things at the same time, and succeeding at neither
You can either read each character of user input one at a time, and use isxdigit to validate it, which is what you would do using the %c conversion, and isxdigit().
Or you can use the %x conversion to validate and convert the user input directly, and skip isxdigit altogether (the scanf will validate it for you).
Here is the latter
Code:
#include <stdio.h>
int main (int argc, char **argv)
{
int a,b,c,r;
printf ("Two numbers please.\n");
r = scanf ("%x%x", &a, &b);
if ( r == 2)
{
c = a + b;
printf ("%#x", c);
}
else
{
printf("hex please");
}
return 0;
}
scanf tells you how many values it converted - if it isn't 2, it wasn't all hex.
-
WOW! Thanks Salem that did the trick