Get rid of trailing newline for scanf

T

Till Crueger

Hi,
I have a little problem with the following code:

#include <stdio.h>

int main(void) {
char input='\0';
while(input!='q') {
printf("Menu\n");
fflush(stdout);
scanf("%c",&input);
/* do something */
}
}

After I get an input there is still a trailing newline. However I always
thought the next scanf would consume this newline. I checked with the FAQ,
but all I found was section 12.18 which covers this issue a bit. The
solution it gives is to only use scanf(), which in this case I do. Any
other hints on what to do about this?
Thanks,
Till
 
S

S.Tobias

Till Crueger said:
Hi,
I have a little problem with the following code:
#include <stdio.h>
int main(void) {
char input='\0';
while(input!='q') {
printf("Menu\n");
fflush(stdout);
scanf("%c",&input);
Try:
scanf(" %c", &input);
(leading space will eat whitespace in the input)
/* do something */
}
}
After I get an input there is still a trailing newline. However I always
thought the next scanf would consume this newline.

scanf is a very determinate function, it won't do anything unless
you tell it to.
I checked with the FAQ,
but all I found was section 12.18 which covers this issue a bit. The
solution it gives is to only use scanf(), which in this case I do. Any
other hints on what to do about this?

scanf is perhaps not good for every task, especially not for user input
(scanf is for _formatted_ input; user must be very orderly to feed it
with the right data). To see this try:
Menu
abcd<Enter>
Menu
Menu
Menu
Menu
I think in general fgets() is better. But for simple cases like above
(where the code is not a "production" code), scanf() is all right
too, and maybe easier to use (if you know what you're doing).

As a learning exercise, try:
scanf("%c ", &input); //space is after conversion specifier
printf("entered: %c\n", (int)input);
and explain the funny effect. Good luck!
 
S

S.Tobias

scanf is a very determinate function, it won't do anything unless
you tell it to.

Argh, just remembered something, which is a must-know.
Whitespace in the input string is eaten at each conversion
specification *except* at %c, %n and %[.
 
R

Raymond Martineau

Hi,
I have a little problem with the following code:

#include <stdio.h>

int main(void) {
char input='\0';
while(input!='q') {
printf("Menu\n");
fflush(stdout);
scanf("%c",&input);
/* do something */
}
}

After I get an input there is still a trailing newline. However I always
thought the next scanf would consume this newline. I checked with the FAQ,
but all I found was section 12.18 which covers this issue a bit. The
solution it gives is to only use scanf(), which in this case I do. Any
other hints on what to do about this?

There's another way of parsing input:
char buffer[80];
int length = 70;
fgets(buffer, length, stdin);
sscanf(buffer, "...");

This will require you to parse input on a line-by-line bases, but should
otherwise function identically.

(I'm really suprised this doesn't appear in the FAQ - I've seen this issue
arise enough times that it should. Either that, or it's not immediatly
visible.)
 
J

Jonathan Burd

Till said:
Hi,
I have a little problem with the following code:

#include <stdio.h>

int main(void) { Ok.
char input='\0';
while(input!='q') {
printf("Menu\n");
fflush(stdout);
scanf("%c",&input);
/* do something */
}
}
[...]
Forgot something? Your `main()` function isn't returning an integer.
Secondly, using `fgets` and `sscanf` is a better approach, as shown
earlier by Raymond.

Regards,
Jonathan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top