Reading a space character using scanf

This is a discussion on Reading a space character using scanf within the C Programming forums, part of the General Programming Boards category; Hi, I've been trying to read to a string with spaces using the following Code: scanf("%[^\n]%*c", student->name); However, it is ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    7

    Reading a space character using scanf

    Hi, I've been trying to read to a string with spaces using the following

    Code:
    scanf("%[^\n]%*c", student->name);
    However, it is not working since it doesn't let me input any thing and just moves on to the next command... any ideas on what I might be doing wrong?

    Thanks in advance!!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    "%c" is for a single character. Use "%s" instead.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    Quote Originally Posted by GReaper View Post
    "%c" is for a single character. Use "%s" instead.
    I tried it but it's still not working... it's not letting me enter an input :/

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    Quote Originally Posted by eleceng16
    However, it is not working since it doesn't let me input any thing and just moves on to the next command... any ideas on what I might be doing wrong?
    I did a quick test: it works for me, assuming I don't deliberately cause buffer overflow. Perhaps you simply have a new line left in the buffer before the scanf.

    EDIT:
    Quote Originally Posted by GReaper
    "%c" is for a single character. Use "%s" instead.
    Considering the *, my guess is that %c was intended to read and discard the new line.
    Last edited by laserlight; 04-26-2012 at 11:29 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    Is there any way I can make sure the buffer is cleared? I have used it before but somehow now its now working...

    EDIT : *problem solved* - laserlight thanks for your help!!! I used %*c%[^\n]%*c instead
    Last edited by eleceng16; 04-26-2012 at 11:40 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    It depends on what code you have for input prior to this scanf.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    If your input stream prior to calling this function does not have a newline, then your code will throw away what could be a valid character of the user's name. You can deal with this by simply putting a space in the format string instead of that leading %*c:
    Code:
    scanf(" %[^\n]%*c", student->name);
           ^ Notice space here
    This leading space will discard any whitespace characters (space/newline/tabs) if present before then starting to read legitimate characters composing the name.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf , char, newline, space
    By Harry Reve in forum C Programming
    Replies: 1
    Last Post: 09-11-2011, 07:49 AM
  2. white space characters and scanf
    By bthomson900 in forum C Programming
    Replies: 4
    Last Post: 11-10-2010, 06:12 AM
  3. Replies: 12
    Last Post: 04-07-2007, 12:11 PM
  4. scanf - strtok and space as delimiter?
    By Bill Cosby in forum C Programming
    Replies: 6
    Last Post: 09-20-2004, 07:45 PM
  5. How to use -- scanf to accept space?
    By leena_teoh in forum C Programming
    Replies: 4
    Last Post: 02-07-2002, 11:22 AM

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