Thread: Weird putchar() glitch

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    3

    Question Weird putchar() glitch

    So I've been working through C Primer Plus and I've run into a strange issue with one of the exercises.

    The idea of the exercise was to make a basic calculator with menu and make it as bullet proof as possible.. Everything works fine except for the function designed to prompt and validate the calculator's input numbers.

    Code:
    float getNum(char prompt[])
    {
        float num;
        char ch;
    
    
        printf("%s", prompt);
        while(scanf("%f", &num) != 1)  //If input not a valid float
        {
            while((ch = getchar()) != '\n')
                putchar(ch);      // Print each character entered up to '\n'
            printf(" is not a valid number.\n");
            printf("%s", prompt);
        }
        while(getchar() != '\n')  // Clear input buffer
            continue;
    
    
        return num;
    }
    What's weird is it seems to work for all input except for the following characters in parenthesis. (+-.ni) If +, -, or . are entered first the first character simply doesn't print.. ++ however prints a single +. n and i are even weirder though they seem to act like an escape and neither they nor the character which follow it will print... nnn or inn however prints a single n. I am completely baffled by this.. Does anyone have any idea what I seem to be missing?
    Last edited by mattmcan; 11-21-2011 at 04:33 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    1) scanf echos the text to the screen as you type... there's no need to print it again.

    2) your putchar on line 11 is going to do nothing of value since the only thing left in the buffer is going to be whatever scanf() didn't consume in converting the input and the user doesn't need to see that.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    Quote Originally Posted by CommonTater View Post
    1) scanf echos the text to the screen as you type... there's no need to print it again.

    2) your putchar on line 11 is going to do nothing of value since the only thing left in the buffer is going to be whatever scanf() didn't consume in converting the input and the user doesn't need to see that.
    My idea was to print out a line illustrating what they entered was not valid input such that the output was similar to the following.. In the following Bold is user text.

    Enter first Number: Pizza
    Pizza is not a valid number.
    Enter first Number:

    It's not really essential that I print that line, so I could just get rid of it.. Mostly at this point though it's an intellectual curiosity I just can't let go of.. Why is it doing such weird things and all. I'm getting stuff like the following.

    Enter first Number: a
    a is not a valid number.
    Enter first Number: -
    is not a valid number.
    Enter first Number: -n
    n is not a valid number.
    Enter first Number: na
    is not a valid number.
    Enter first Number: naa
    a is not a valid number.
    Enter first Number:
    Last edited by mattmcan; 11-21-2011 at 04:29 AM.

  4. #4
    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.

  5. #5
    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..

  6. #6
    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