Thread: Scanf with spaces

  1. #1
    Bajanine
    Guest

    Scanf with spaces

    From the book 'Teach Yourself C' by Herbert Schildt page 250 it says to allow spaces to be input using the scanf function use a scanset like so:

    scanf("%10[0-9a-zA-Z ]", str);

    I found that this did not work for me as it still wouldn't allow me scan in spaces. So I experimented with the following:

    scanf("%10[0-9a-zA-Z\ ]", str);

    This line works but my compiler issues a warning:

    warning C4129: ' ' : unrecognized character escape sequence.

    My compiler is MSV C++6.0 on a XP system.

    What is the correct way to do this?

    Thanks in advance!

  2. #2
    Bajanine
    Guest

    Smile I figured it out!

    The book left out the 's' denoting a string. After looking it over one final time I asked myself "How does it know it is a string versus say a number."

    The book should of read:

    scanf("%10[0-9a-zA-Z ]s", str);

    Imagine that it compiles without errors or warnings.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >scanf("%10[0-9a-zA-Z ]", str);
    In his unlimited ignorance, Mr. Schildt has made several errors in one line of code. The first being the use of scanf for this kind of input in the first place, it is buggy and cranky and should be avoided in favor of fgets. Second is the use of the 0-9 trick to specify a range, this is not portable and will break on compilers that do not support it. Third is the use of a magic number, required for this use of scanf, but it tends to be a bad practice. Once again I direct your attention away from scanf and toward fgets:
    Code:
    fgets ( str, sizeof str, stdin );
    >The book left out the 's' denoting a string.
    That isn't needed, but the format string rules for scanf are confusing to say the least, try fgets instead. Notice a trend?

    -Prelude
    My best code is written with the delete key.

  4. #4
    Bajanine
    Guest

    ????

    So you say that the 's' isn't needed. I will certainly take your word for it.

    I am going through this book again because it has been some time since I attempted any programming. I have very little experience as I am sure it shows. So may I ask is there anytime in which to use scanf?

    Thanks Prelude, I appreciate your input greatly.

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Herbert Schlidt works for Hamas and Alcada.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing 3 spaces to tabs
    By dnguyen1022 in forum C Programming
    Replies: 2
    Last Post: 12-22-2008, 12:51 AM
  2. Tabs or Spaces
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 04-08-2007, 11:45 AM
  3. saving a CString to binary file with trailing spaces
    By nineofhearts in forum C++ Programming
    Replies: 12
    Last Post: 01-03-2006, 11:53 AM
  4. Newbie problem with scanf function...
    By Mithal in forum C Programming
    Replies: 1
    Last Post: 11-13-2005, 10:28 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