scanf

This is a discussion on scanf within the C Programming forums, part of the General Programming Boards category; I can't for the love of god understand why scanf doesnt work. here's the code: Code: void main () { ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    16

    scanf

    I can't for the love of god understand why scanf doesnt work.
    here's the code:

    Code:
    void main ()
    {
       char prev_move[3], next_move[2], new_move;
    
       printf("Press 'y' for a new move or anything else to exit: ");   scanf(" %c",&new_move);
       
       while(new_move == 'y')
       {
          printf("\nchoose the pawn you want to move and it's current position: ");
          scanf(" %s",&prev_move);
          printf("\nchoose next move: ");
          scanf(" %s",&next_move);
           
          for(int i=0; i<3; i++)
               printf("%c",prev_move[i]);               
       }
    
    }

    prev_move is a 3 positions char array. for prints 3 chars of which the first is a whitespace, and the the other two that I have entered from the keyboard. Scanf seems to place a whitespace in prev_move[0]. Why is this happening?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    scanf works perfectly well, you are probably just misunderstanding some details on how it works. You should read the documentation for scanf to understand exactly what it's doing.


    1. Drop the & from the scanf calls for prev_move and next_move. The name of the array serves as a pointer to the first element, so you don't need it with char arrays and the %s modifier.
    2. You should check the return value of scanf and make sure it successfully converted whatever you typed. It will return the number of successfully converted items, i.e. 1 for each of the calls you have. If it returns anything else, there's a problem.
    3. You don't use the width modifier, e.g. " %2s", to ensure you don't overflow prev_move or next_move. Once you overflow, you're in undefined behavior and anything can happen, including placing a whitespace in prev_move[0]. I usually prefer fgets for reading strings from the user, I recommend you look into it. You also may want to make those buffers a little bigger, just to be safe.
    4. You could save yourself the trouble of the for loop for printing, and just call printf("%s", prev_move);
    5. The correct form is int main(void) and you should return an int at the end, usually 0 for success.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    Well next_move can legally only hold a single character before you get buffer overflow (the other being \0).
    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.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    Is there any way to clear the buffer before the second scanf ?
    I don't want to change the arrays size because I use them somewhere else in my program

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Quote Originally Posted by mnml View Post
    Is there any way to clear the buffer before the second scanf ?
    You can try, but I don't think that's your problem. The %s modifier skips all leading white space, so prev_move and next_move should never have any white space. Still, if you're interested: Cprogramming.com FAQ > Flush the input buffer
    I don't want to change the arrays size because I use them somewhere else in my program
    Well, if you need the size to be consistent everywhere, you should use a named constant, regardless of whether you change the size. Like so:
    Code:
    #define PREV_MOVE_MAX   3
    #define NEXT_MOVE_MAX   2
    ...
    char prev_move[PREV_MOVE_MAX], next_move[NEXT_MOVE_MAX];

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    I can't believe I'm struggling with this 2 hours now. I've tried several methods with no luck.
    Do you mean I should not have a whitespace between " and % inside scanf ?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    The whitespace between " and %s doesn't matter, because %s ignores leading white space. You can not use %s if you need to read in white space. What is your new code, and what exactly are you entering as input?

  8. #8
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    On this board, posts are lengthy for a reason. grumpy gave you all the information you need, have you read it?
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    I think I figured it out. All I had to do was increase the array's size by one.
    I'm entering 3 chars to prev_move uppercase lowercase and a number, for example Pa2 and a2 to next_move.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    Thanks a lot for all your replies by the way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf
    By Ciaran789 in forum C Programming
    Replies: 4
    Last Post: 11-22-2009, 06:40 AM
  2. while scanf
    By PUI in forum C Programming
    Replies: 3
    Last Post: 10-06-2006, 11:24 AM
  3. First scanf() skips next scanf() !
    By grahampatten in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:47 AM
  4. scanf
    By Gsibbery in forum C Programming
    Replies: 7
    Last Post: 08-08-2003, 09:03 PM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM

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