question about reading inputs starting with 0

This is a discussion on question about reading inputs starting with 0 within the C Programming forums, part of the General Programming Boards category; i know this may seem odd but i want to read in an input starting with a 0 its a ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    28

    question about reading inputs starting with 0

    i know this may seem odd but i want to read in an input starting with a 0
    its a menu selection
    the menu options need to be

    100000
    100010
    000010
    000011

    Code:
    void readnum(int a, int b)
     {
       unsigned int code = 0;
    
       printf(options);
       scanf("%u", &code);
    
       while (code != 100000 && code !=100010 && code !=000010 && code !=000011)
       {
         printf(options);
         scanf("%u", &code);
       }
    
       if (code == 100000)
        add(a, b);
    
       if (code == 100010)
        sub(a, b);
    
       if (code == 000010)
        mult(a, b);
    
       if (code == 000011)
        div(a, b);
     }
    it works for the numbers starting with a 1, but not the numbers starting with a 0.
    thanks!

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    That's because, by default, the compiler thinks you're using a decimal system, and it looks like you're using binary (or trying to). You'll need to convert all the numbers to hex and then precede each one with 0x.

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    To give you a little help:

    000010 = 0x2
    000011 = 0x3
    100000 = 0x20
    100010 = 0x22

  4. #4
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by jibbles
    i know this may seem odd but i want to read in an input starting with a 0
    its a menu selection
    the menu options need to be

    100000
    100010
    000010
    000011
    Beware. Constant literals beginning with 0 are considered 'octal' by the compiler.

    000010 = 010 (octal) = 8 (decimal)

    (BTW, there is no 'binary' literal representation in C. Use hexadecimal or octal)

    octal is nice for 6 bits (2 sets of 3):

    100 000 = 040
    100 010 = 042
    000 010 = 002
    000 011 = 003

    but hex is nice too and less confusing IMO.
    Emmanuel Delahaye

    "C is a sharp tool"

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > if (code == 100000)
    This is a decimal constant (base 10)

    > if (code == 000010)
    This is an octal constant (base 8)

    If you really want to input and compare binary, then you'll need to do it using strings.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    i worked it out, i could just use 11 and 10 to represent 000011 and 000010 shifty little trick but it did what i needed.

    i'd just like to thank you all for the help you've given me over the last couple of days. i got my program working as good as needed.

    THANK YOU!

    i've also got alot more interested in C programming.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It seems everyone overlooked an obvious solution:
    1) Change "code" into a string.
    2) Compare them to your "six bit number":
    Code:
    if( strcmp( input, "100000" ) == 0 )
    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Lightbulb Salem did not overlook....

    Quote Originally Posted by quzah
    It seems everyone overlooked an obvious solution:
    1) Change "code" into a string.
    2) Compare them to your "six bit number":
    Code:
    if( strcmp( input, "100000" ) == 0 )
    Quzah.
    HI Quzah,
    Salem DID write that in his Response. .. (only he didnt quote an example)

    -Harsha
    Help everyone you can

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by vsriharsha
    HI Quzah,
    Salem DID write that in his Response. .. (only he didnt quote an example)

    -Harsha
    I'm going back to bed.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in reading inputs
    By Bargi in forum C Programming
    Replies: 15
    Last Post: 06-18-2008, 10:28 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:59 PM
  3. Replies: 2
    Last Post: 01-28-2008, 03:07 AM
  4. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 03:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21