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??) 11 28280
"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
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.
"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
"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
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
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.
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).
"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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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);
|
by: Vinicius |
last post by:
Hello,
the following code does not work:
"
....
void main(void)
{
char option;
printf("Choose an option: ");
|
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?
|
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?
|
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};
| |
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');
|
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);
|
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
|
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
{
|
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...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |