Odd loop/printf behavior in C

Discussion in 'Mac Programming' started by Sam Yikin, Jul 24, 2008.

  1. Sam Yikin macrumors regular

    Joined:
    Oct 28, 2007
    #1
    So, I was testing my understanding of the syntax for do-while loops and the toupper() function and ran into an odd error.
    Code:
    #include <stdio.h>
    
    int main () {
     char p;
     int i=0;
     do {
    	printf ("Type something in please: ");
    	scanf ("%c", &p);
    	p=toupper(p);
    	printf ("%c\n", p);
    	i+=1; 
        } while (i<10);
     return 0;
     }
    What is happening is, after the initial run it is printing "Type something in please:" and the newline in the printf that displays the result is printing twice. I suspect this has something to do with the keyboard buffer (still working on learning about that, so explain it in basic terms please :))
    Can anyone help me with a workaround/fix for this?
     
  2. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #2
  3. angryboffin macrumors newbie

    Joined:
    May 7, 2008
    Location:
    London
    #3
    Too late

    I was beaten to it because I'd forgotten my password. And because all my C programming involved multiplying matrices and Fourier transforms. So I had to look this up. Anyway, in addition to Todd's helpful (and prompter) link, you might find this link useful:

    http://www.gidnetwork.com/b-60.html

    Regards,

    Joshua
     
  4. Sam Yikin thread starter macrumors regular

    Joined:
    Oct 28, 2007
    #4
    Thank you, that was very helpful.
    So can anyone help me see if I understand this:
    When you first something in (for example, t) the t is sent to the keyboard buffer and NOT directly to the program.
    It is only sent to the program when the user presses Enter/Return. Pressing Enter adds a newline character to the buffer. However, enter is pressed, only ONE of the characters is actually sent to the program. The t is sent to the program. Thus, the scanf() function ONLY gets the first character- the t. The newline key, still in the buffer, is gotten by the scanf() function when the loop repeats, causing the input to appear blank. This can be worked around by adding a getch() after the first input to "snag" the newline.
     
  5. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #5
    Yes, that is correct.

    If you were to enter

    abcd\n

    The loop would repeat 5 times.

    This is all predicated on the fact that you told scanf() to use a single character (%c) in the format specifier.

    Todd
     
  6. Sam Yikin thread starter macrumors regular

    Joined:
    Oct 28, 2007
    #6
    Thank you again.
    Also, does that mean that this could be worked around by making it a string instead of a single character?
     
  7. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #7
    If you make a string, you'll still have to deal with the newline, unless you specify a different type of formatter.
     
  8. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #8
    You should consider thinking about interactive user input in terms of "lines", not of "characters" - even if those "lines" actually only contain (or should contain) a single character plus newline.
     
  9. Sam Yikin thread starter macrumors regular

    Joined:
    Oct 28, 2007
    #9
    Oh, that cleared a few things up... thank you.
     

Share This Page