473,480 Members | 2,333 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Get rid of trailing newline for scanf

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

--
Please add "Salt and Peper" to the subject line to bypass my spam filter

Nov 14 '05 #1
4 9452
Till Crueger <Ti****@gmx.net> wrote:
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!

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #2
S.Tobias <si***@famous.bedbug.pals.invalid> wrote:
Till Crueger <Ti****@gmx.net> wrote:
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.


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 %[.

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #3
On Sun, 16 Jan 2005 14:04:55 +0100, Till Crueger <Ti****@gmx.net> wrote:
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.)

Nov 14 '05 #4
Till Crueger wrote:
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.

--
"If unsigned integers look like two's complement (signed) integers,
it's because two's complement integers look like unsigned integers."
-Peter Nilsson
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3489
by: Maxime Biais | last post by:
Hello, I know how to print a string without the trailing newline with a coma like : print "hello word", but this example add a white space after the string :( Do you know how to avoid...
14
2703
by: Peter Mount | last post by:
Hello I'm having trouble with " scanf("%c", &answer);" on line 20 below. When I run the program in cygwin on Windows 98SE it skips that line completely and ends the program. Does scanf have...
7
7636
by: hugo27 | last post by:
obrhy8 June 18, 2004 Most compilers define EOF as -1. I'm just putting my toes in the water with a student's model named Miracle C. The ..h documentation of this compiler does state that when...
3
5798
by: Paul | last post by:
Hi, My RichTextBox has multiple lines of text. Most of the lines unfortunately end with a space. Is it possible to replace the space and NewLine/Line Feed with just the NewLine/LineFeed? So...
1
8477
by: linq936 | last post by:
Hi, I read in many places that the string to be outputted by printf() must be ending with newline, for example, it should be printf("Hello World.\n"); instead of printf("Hello World.");
2
7980
by: Russell Warren | last post by:
I was just setting up some logging in a make script and decided to give the built-in logging module a go, but I just found out that the base StreamHandler always puts a newline at the end of each...
1
12593
by: toefraz | last post by:
Hello, all. I am writing a patch of code that displays the time and date in the middle of the line. I am using the asctime function from ctime but I don't want the trailing newline character after...
4
22300
by: lihao0129 | last post by:
Hi, folks: I recently went through a strange problem with my Javascript code, say: I have a string variable which are from a 'textarea' element and I want to remove the trailing newlines inside...
5
2934
by: brian.j.parker | last post by:
Hey all, I've noticed an obscure little quirk: it appears that if you use a login with trailing spaces on the name, SYSTEM_USER automatically trims those trailing spaces in SQL Server 2000, but not...
0
6915
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6750
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4794
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4493
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3003
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2993
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
567
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.