Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Sam Yikin

macrumors regular
Original poster
Oct 28, 2007
230
0
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?
 

angryboffin

macrumors newbie
May 7, 2008
14
0
London
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
 

Sam Yikin

macrumors regular
Original poster
Oct 28, 2007
230
0
Yes. What is happening is that there are 2 characters in the input buffer when you press ENTER: the lowercase character and the NEWLINE character.

After you scanf() the lowercase char, you can do a getchar() (or a loop of getchar()s) until the NEWLINE is removed from the input buffer.

See this link: http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044873249&id=1043284392

Todd
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.
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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
 

Sam Yikin

macrumors regular
Original poster
Oct 28, 2007
230
0
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

Thank you again.
Also, does that mean that this could be worked around by making it a string instead of a single character?
 

Sander

macrumors 6502a
Apr 24, 2008
520
67
Thank you again.
Also, does that mean that this could be worked around by making it a string instead of a single character?

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.
 

Sam Yikin

macrumors regular
Original poster
Oct 28, 2007
230
0
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.

Oh, that cleared a few things up... thank you.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.