473,800 Members | 2,499 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

new line character : scanf and getchar

char c;
int i;

scanf("%d",&i);
c=getchar();

I want to read a integer and a character in 2 lines.
Why the getchar always get the \n character from the last line?????

(Sorry for my newbie question, but where is the FAQ section of the
newsgroup??)
Nov 14 '05 #1
11 28280
j

"Mars" <Mars@Mars> wrote in message news:41******** **@rain.i-cable.com...
char c;
int i;

scanf("%d",&i);
c=getchar();

I want to read a integer and a character in 2 lines.
Why the getchar always get the \n character from the last line?????

In your case, stdin refers to an interactive device and as such, is line
buffered.
This means that data is transferred when a newline is encountered.

If scanf's conversion succeeds you will have a newline left in your input
stream.
Any subsequent read issued on stdin will encounter this newline if you did
not enter additional data after some integer that corresponds to your
conversion
specifier.

One solution would be,

void consume_stdin(v oid)
{
while((getchar( )) != '\n')
;
}
You should also do some error checking with respect to scanf's return value.
(Sorry for my newbie question, but where is the FAQ section of the
newsgroup??)


http://www.eskimo.com/~scs/C-faq/faq.html

--
j
Nov 14 '05 #2
Why don't you just write

int i;
char c;

scanf("%d",&i);
fflush(stdin); //flushes the input stream and gets rid of '\n'
character
c=getchar();

Try it out, it will work.

Nov 14 '05 #3
j

"Sontu" <ab******@gmail .com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.com.. .
Why don't you just write

int i;
char c;

scanf("%d",&i);
fflush(stdin); //flushes the input stream and gets rid of '\n'
character
c=getchar();

Try it out, it will work.


Supposing that there is data in the input stream, where do you propose
that it will be flushed to?

This is actually addressed in the FAQ but I want you to answer my question.

--
j
Nov 14 '05 #4
"Sontu" <ab******@gmail .com> wrote:
Why don't you just write
Why doesn't _who_ just write that? Leave in some context when you post!
If you must use Google-broken-beta, learn to use it properly.
int i;
char c;

scanf("%d",&i);
fflush(stdin); //flushes the input stream and gets rid of '\n'
Because the behaviour fflush() is undefined for input streams. It only
works for output streams.
Try it out, it will work.


Says you. On your edition of your favourite compiler, it may even do
something. It's not required to do anything sane or safe, though.

Richard
Nov 14 '05 #5
Sontu wrote:

Why don't you just write

int i;
char c;

scanf("%d",&i);
fflush(stdin); //flushes the input stream and gets rid of '\n'
character
c=getchar();

Try it out, it will work.


No it won't (portably). It may assasinate somebody. Please don't
give incorrect advice around here. You should have looked up the
validity of fflush for input devices (which is none).

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #6
Mars <Mars@Mars> writes:
char c;
int i;

scanf("%d",&i);
c=getchar();

I want to read a integer and a character in 2 lines.
Why the getchar always get the \n character from the last line?????


The getchar() reads the '\n' character because the scanf() didn't read it.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7
j wrote:
"Mars" <Mars@Mars> wrote:
char c;
int i;

scanf("%d",&i);
c=getchar();

I want to read a integer and a character in 2 lines.
Why the getchar always get the \n character from the last line?????

In your case, stdin refers to an interactive device and as
such, is line buffered. This means that data is transferred
when a newline is encountered.


Irrelevent, the same problem occurs on any stream. All you can
deduce from this is that there was a newline character after
the number.
Exactly the same thing would have happened if stdin was coming
from a non-interactive device (eg. a file), so I don't know
how you can say that it did refer to an interactive device.
If scanf's conversion succeeds you will have a newline left in
your input stream.
Any subsequent read issued on stdin will encounter this newline
if you did not enter additional data after some integer that
corresponds to your conversion specifier.


Subsequent reading will always encounter the newline (eventually).
(I think this is what you intended to say, although on the first
reading, it seemed to me that you were saying that if additional
data was entered then the newline would not be encountered).

Nov 14 '05 #8
j

"Old Wolf" <ol*****@inspir e.net.nz> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
j wrote:
"Mars" <Mars@Mars> wrote:
char c;
int i;

scanf("%d",&i);
c=getchar();

I want to read a integer and a character in 2 lines.
Why the getchar always get the \n character from the last line?????


In your case, stdin refers to an interactive device and as
such, is line buffered. This means that data is transferred
when a newline is encountered.


Irrelevent, the same problem occurs on any stream. All you can
deduce from this is that there was a newline character after
the number.
Exactly the same thing would have happened if stdin was coming
from a non-interactive device (eg. a file), so I don't know
how you can say that it did refer to an interactive device.


Yeah, I'm not sure what I was thinking there.
If scanf's conversion succeeds you will have a newline left in
your input stream.
Any subsequent read issued on stdin will encounter this newline
if you did not enter additional data after some integer that
corresponds to your conversion specifier.


Subsequent reading will always encounter the newline (eventually).
(I think this is what you intended to say, although on the first
reading, it seemed to me that you were saying that if additional
data was entered then the newline would not be encountered).


I was thinking in terms of what he presented.
He just included one call to getchar after scanf.

Something such as ``10aaaaaa\n''
would have yielded a valid conversion
with a subsequent read producing ``a''.

--
j
Nov 14 '05 #9
T O


Old Wolf wrote:
j wrote:
"Mars" <Mars@Mars> wrote:
char c;
int i;

scanf("%d",& i);
c=getchar( );

I want to read a integer and a character in 2 lines.
Why the getchar always get the \n character from the last line?????


In your case, stdin refers to an interactive device and as
such, is line buffered. This means that data is transferred
when a newline is encountered.

Irrelevent, the same problem occurs on any stream. All you can
deduce from this is that there was a newline character after
the number.
Exactly the same thing would have happened if stdin was coming
from a non-interactive device (eg. a file), so I don't know
how you can say that it did refer to an interactive device.

If scanf's conversion succeeds you will have a newline left in
your input stream.
Any subsequent read issued on stdin will encounter this newline
if you did not enter additional data after some integer that
corresponds to your conversion specifier.

Subsequent reading will always encounter the newline (eventually).
(I think this is what you intended to say, although on the first
reading, it seemed to me that you were saying that if additional
data was entered then the newline would not be encountered).


Taking this a step further then, what would be a good but simple method
of reading a single character from stdin, evaluating that character (if
c=="Y") for example, and then flushing all buffers.
Is there a simple way to achieve a yes/no response without requring a CR
from the user, thereby presumably negating this problem of /n.
Nov 14 '05 #10

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

Similar topics

6
6221
by: Gus Tabares | last post by:
Hello, I'm having trouble reading in a character. Here is a snippet of code: int num; char character; printf("Enter a number: "); scanf("%d", &num); printf("You entered %d.\n", num);
3
2759
by: Vinicius | last post by:
Hello, the following code does not work: " .... void main(void) { char option; printf("Choose an option: ");
17
3505
by: Lefty Bigfoot | last post by:
Hello, I am aware that a lot of people are wary of using scanf, because doing it improperly can be dangerous. I have tried to find a good tutorial on all the ins and outs of scanf() but been unsuccessful. Is there a well-respected (by the c.l.c crowd) book or tutorial that really covers scanf in detail?
7
13266
by: gyan | last post by:
I want to read a line with white spaces though scanf. So i used: scanf("%",string); above is working in one program, but in other..what may be the reason?
9
4012
by: kernelxu | last post by:
hi,everybody. I calling function setbuf() to change the characteristic of standsrd input buffer. some fragment of the progrem is: (DEV-C++2.9.9.2) #include <stdio.h> #include <stdlib.h> int main(void) { char buf = {0};
25
5447
by: ehabaziz2001 | last post by:
Why I can not begin my subscript of character arrrays with 0. In this program I can not do : do { na=getchar(); i++; na=getchar(); } while (na!='\n');
9
2694
by: Cao Yi | last post by:
Hi, here's a fract of codes, and what's the line "scanf("%lf%*", &cvi)" doing? ============================= do { printf("\nCoefficient: "); scanf("%lf%*", &cvi); getchar(); } while (cvi <= 0.0);
26
4545
by: tesh.uk | last post by:
Hi Gurus, I have written the following code with the help of Ivor Horton's Beginning C : // Structures, Arrays of Structures. #include "stdafx.h" #include "stdio.h" #define MY_ARRAY 15
6
2113
by: ehabaziz2001 | last post by:
My Program turned a char variable into string using null character but unexpected character displayed infront of the variable after using printf . Syntax : ------------- #include <stdio.h> struct record {
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10504
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10274
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10251
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10033
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7576
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5469
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.