Thread: Weird putchar() glitch

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The weirdness you are seeing is scanf() consuming parts of the entry to decide if it should proceed or fail the conversion...

    The minus sign might be part of a valid number.
    nan is a valid floating point value so na is consumed before it fails
    Same with the last example naa starts with na which is consumed before it fails.

    Generally there's no reason to be so verbose with users. They can see what they entered so just give them an error report such as printing Error : before the next prompt line.

    Enter first number antidisestablishmentarianism
    ERROR : Enter first number :

    One thing people really hate is having to actually read the screen in front of them... Keep your user prompts short and sweet or they will simply ignore them.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    Ahh thank you.. That makes sense, I sorta thought it might be that with the +- and . but I couldn't figure out why (n or i) would do it... One thing though.. n and i also seem to consume whatever the next character is regardless.. For instance nml, would still only print l.. Shouldn't it only consume the second character if it's an a?

    I agree with you in terms of Verbosity. I only did it this way so that I might copy the exercise example precisely. However it does beg the question what would one do if they needed all of the user's incorrect input for another reason.. As it stands I would be unable to use it if it started with any of those characters..

    Thanks again..

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mattmcan View Post
    Ahh thank you.. That makes sense, I sorta thought it might be that with the +- and . but I couldn't figure out why (n or i) would do it... One thing though.. n and i also seem to consume whatever the next character is regardless.. For instance nml, would still only print l.. Shouldn't it only consume the second character if it's an a?
    It needs to consume the second character to know the conversion fails... NAN vs NNA you can't tell without looking at the second letter. NAV would consume the entire 3 letters to know it failed.

    I agree with you in terms of Verbosity. I only did it this way so that I might copy the exercise example precisely. However it does beg the question what would one do if they needed all of the user's incorrect input for another reason.. As it stands I would be unable to use it if it started with any of those characters..

    Thanks again..
    In that situation you would probably want to load the user input into a text buffer (string) with fgets() parse the text for suitability then pass it to sscanf() for conversion. This would allow you to log bad inputs simply by copying the buffer.
    Last edited by CommonTater; 11-21-2011 at 05:09 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird cin.getline() glitch
    By Arieken in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2011, 10:10 AM
  2. weird google glitch?
    By stevesmithx in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 02-01-2009, 01:11 AM
  3. SmartFTP glitch?
    By alphaoide in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 02-27-2004, 08:37 PM
  4. Okay its running with a serious glitch
    By Blizzarddog in forum C++ Programming
    Replies: 2
    Last Post: 11-13-2002, 02:33 PM
  5. Wheres the glitch?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-03-2001, 10:26 AM