Thread: Reading a space character using scanf

  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
    2,739
    "%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
    28,413
    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 10:29 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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 10:40 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It depends on what code you have for input prior to this scanf.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,817
    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, 06: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, 11:11 AM
  4. scanf - strtok and space as delimiter?
    By Bill Cosby in forum C Programming
    Replies: 6
    Last Post: 09-20-2004, 06: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