#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Beginnerīs array problem


    Hello,

    I have probably a simple problem, but as a beginner I canīt really solve this.

    On the input there is a sequence of hexadecimals separated by spaces and ended with the '0' char. I want to get them into an unsigned int array.

    This does not work:

    Code:
    unsigned int input[99];
    int i = 0;
    unsigned int character;
    
    while((character = getchar()) != '0') {
        scanf("%X", &(input[i]));
        i++;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    Try ungetc().

    getchar() removes one character from the input stream. If you want to read that character with scanf() you have to put it back in the stream.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0
    getchar() removes one character from the input stream. If you want to read that character with scanf() you have to put it back in the stream.
    I want to read the whole hexadecimal number, store it in the array[0], then read another, store it in the array[1] and so on, until I type '0'.
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,365
    Rep Power
    1870
    So post an example of what you want to type in?

    Say
    0x1234
    0x5678
    0xabcd
    0


    Puts 3 numbers into input, and sets i to 3.
    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
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0
    I put something like "AAA66AA6 0", or "AAA66AA6 595A966A 0" for example. All the values are in the same line.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    You would need to test for both that your index variable i doesn't exceed the array size, that is if 99<=i you must break from the loop, and if input[i] is 0 then you must also exit the loop. As you have written your code, the character following an entry is always a space character of some sort. Your code works only in the important but not so interesting case that the first character from stdin is a zero digit.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,365
    Rep Power
    1870
    Code:
    while ( i < 99 && scanf("%X", &input[i] ) == 1 && input[i] != 0 ) {
      i++;
    }
    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
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0
    Originally Posted by salem
    Code:
    while ( i < 99 && scanf("%X", &input[i] ) == 1 && input[i] != 0 ) {
      i++;
    }
    When I use it like this:
    Code:
    while ( i < 99 && scanf("%X", &input[i] ) == 1 && input[i] != 0 ) {
      i++;
      printf("0x%X\n", input[i]);
    }
    it still does not work and I do not know why.
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    The problem is that you increment i before printing. So you're displaying uninitialized elements of your array.

    change the order of ++i and printf lines.
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0
    Originally Posted by b49P23TIvg
    The problem is that you increment i before printing. So you're displaying uninitialized elements of your array.

    change the order of ++i and printf lines.
    Thatīs it! Thank you so much.

IMN logo majestic logo threadwatch logo seochat tools logo