Thread: what I'm I doing wrong?

  1. #1
    Unregistered
    Guest

    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;
    }

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    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.
    Last edited by Shiro; 02-02-2002 at 01:17 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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))

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    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.

  5. #5
    Unregistered
    Guest
    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

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  7. #7
    Unregistered
    Guest

    Thumbs up

    WOW! Thanks Salem that did the trick

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM