Thread: program error!

  1. #16
    Registered User
    Join Date
    Mar 2014
    Posts
    15
    Thanks, guys! It works perfectly.
    Now just to satiate my curiosity, why does the variable accept a blank space as a char when I never hit a space??
    Or is it just a formality to leave a blank before the formatting type?

  2. #17
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It doesn't match a whitespace. Check the scanf documentation. In particular
    Quote Originally Posted by man 3 scanf
    The format string consists of a sequence of directives which describe how to process the sequence of input characters.
    ...
    A directive is one of the following:
    • A sequence of white-space characters (space, tab, newline, etc.; see isspace(3)). This directive matches any amount of white space, including none, in the input.
    So a whitespace in the format string means skip whitespace like a new line.

  3. #18
    Registered User
    Join Date
    Mar 2014
    Posts
    15
    @anduril462,
    So it's ok if I use it everywhere, right?
    Thanks!

  4. #19
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by [David] View Post
    @anduril462,
    So it's ok if I use it everywhere, right?
    Thanks!
    Yes...well, it's ok to use everywhere you want to skip over whitespace . Obviously if you don't want to skip whitespace, don't use it.

    Note that one single whitespace char in the format string skips all contiguous whitespace if present. Also note that most scanf format specifiers (all numeric specifiers plus %s) automatically skip whitespace so the space in " %d" is redundant, you can just do "%d".

    I'm not a big fan of using scanf for user input -- especially single-character input -- for this reason, it's easy to make a small mistake that breaks your program.

    A good rule of thumb is to not mix input modes. So character input like getchar() should not be used in conjunction with formatted input like scanf or line-based input like fgets. Note that scanf("%c") -- without the space -- is effectively the same as getchar however.

    This is why I typically avoid scanf for user input, except in the simplest of programs. It's too easy to get scanf stuck on bad user input. I prefer reading line-by-line with fgets. Then I can either use strcmp to look for exact strings, or sscanf (the first 's' is because that version scans formatted strings) to parse out values, or do something else all together.

  5. #20
    Registered User
    Join Date
    Mar 2014
    Posts
    15
    Anyone interested in the updated code?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 11-28-2011, 11:48 AM
  2. Replies: 13
    Last Post: 11-03-2010, 12:45 PM
  3. C Program... Program or Compiler error?
    By Zemira in forum C Programming
    Replies: 13
    Last Post: 12-02-2009, 08:59 PM
  4. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  5. i'm trying to fix an error with my error messaging on my program
    By RancidWannaRiot in forum C++ Programming
    Replies: 10
    Last Post: 09-30-2003, 01:02 PM

Tags for this Thread