473,911 Members | 5,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

scanf problem

I know this is the problem that most newbies get into.

#include<stdio. h>
int main(void)
{
char a;
scanf("%c",&a); /*1st scanf */
printf("%c\n",a );
scanf("%c",&a); /*2nd scanf*/
printf("%c\n",a );
return 0;
}

This will not work, because after the first scanf a newline is left
behind in the input stream, unconsumed.

I made this attempt to solve this problem

#include<stdio. h>
int main(void)
{
char a,b;
scanf("%c",&a);
printf("%c\n",a );
getchar(); /* putting getc(stdin) also worked*/
scanf("%c",&a);
printf("%c\n",a );
return 0;
}

Out of curiosity I tried this also,

#include<stdio. h>
int main(void)
{
char a,b;
scanf("%c",&a);
printf("%c\n",a );
scanf("\n%c",&a );
printf("%c\n",a );
return 0;
}

which gave me expected output.

But my question is (finally!) , why the following code doesn't work,

#include<stdio. h>
int main(void)
{
char a,b;
scanf("%c\n",&a ); /* 1st scanf */
printf("%c\n",a );
scanf("%c",&a); /* 2nd scanf */
printf("%c\n",a );
return 0;
}

My assumption was 1st scanf will consume the newline left in the input
stream.
Thanks for your time,
Yugi.

Sep 8 '06 #1
14 13846
I use fscanf to read from a file and the newline is consumed, using
both MS-VC 6 and gcc 3.3.5. What are you using?

Sep 8 '06 #2
main() wrote:
I know this is the problem that most newbies get into.

#include<stdio. h>
int main(void)
{
char a;
scanf("%c",&a); /*1st scanf */
printf("%c\n",a );
scanf("%c",&a); /*2nd scanf*/
printf("%c\n",a );
return 0;
}

This will not work, because after the first scanf a newline is left
behind in the input stream, unconsumed.

I made this attempt to solve this problem

#include<stdio. h>
int main(void)
{
char a,b;
scanf("%c",&a);
printf("%c\n",a );
getchar(); /* putting getc(stdin) also worked*/
scanf("%c",&a);
printf("%c\n",a );
return 0;
}
This will only work (the way you seem to want it to) if the second
character in the first input line is a newline. You introduced a new
variable here, b, did you mean to do something with it?
Out of curiosity I tried this also,

#include<stdio. h>
int main(void)
{
char a,b;
scanf("%c",&a);
printf("%c\n",a );
scanf("\n%c",&a );
printf("%c\n",a );
return 0;
}

which gave me expected output.
Any whitespace in the format string will match any amount of whitespace
in the input, including none. Your newline will match any number of
spaces, tabs, newlines, etc., until a non-whitespace character is
encountered.
But my question is (finally!) , why the following code doesn't work,

#include<stdio. h>
int main(void)
{
char a,b;
scanf("%c\n",&a ); /* 1st scanf */
printf("%c\n",a );
scanf("%c",&a); /* 2nd scanf */
printf("%c\n",a );
return 0;
}

My assumption was 1st scanf will consume the newline left in the input
stream.
The first scanf call will store the first character into a and consume
of any immediately following whitespace (including newlines and any
whitespace that follows). You didn't specify what the input was that
you used or how the output differed from your expectations, so it's
difficult to help you understand what the "problem" is, feel free to
clarify.

See also my response to the thread entitled "cannot use char and
integer in the same program" on August 10th 2006 in this group.

Robert Gamble

Sep 8 '06 #3

Robert Gamble wrote:
main() wrote:
I know this is the problem that most newbies get into.

#include<stdio. h>
int main(void)
{
char a;
scanf("%c",&a); /*1st scanf */
printf("%c\n",a );
scanf("%c",&a); /*2nd scanf*/
printf("%c\n",a );
return 0;
}

This will not work, because after the first scanf a newline is left
behind in the input stream, unconsumed.

I made this attempt to solve this problem

#include<stdio. h>
int main(void)
{
char a,b;
scanf("%c",&a);
printf("%c\n",a );
getchar(); /* putting getc(stdin) also worked*/
scanf("%c",&a);
printf("%c\n",a );
return 0;
}

This will only work (the way you seem to want it to) if the second
character in the first input line is a newline. You introduced a new
variable here, b, did you mean to do something with it?
Out of curiosity I tried this also,

#include<stdio. h>
int main(void)
{
char a,b;
scanf("%c",&a);
printf("%c\n",a );
scanf("\n%c",&a );
printf("%c\n",a );
return 0;
}

which gave me expected output.

Any whitespace in the format string will match any amount of whitespace
in the input, including none. Your newline will match any number of
spaces, tabs, newlines, etc., until a non-whitespace character is
encountered.
But my question is (finally!) , why the following code doesn't work,

#include<stdio. h>
int main(void)
{
char a,b;
scanf("%c\n",&a ); /* 1st scanf */
printf("%c\n",a );
scanf("%c",&a); /* 2nd scanf */
printf("%c\n",a );
return 0;
}

My assumption was 1st scanf will consume the newline left in the input
stream.

The first scanf call will store the first character into a and consume
of any immediately following whitespace (including newlines and any
whitespace that follows). You didn't specify what the input was that
you used or how the output differed from your expectations, so it's
difficult to help you understand what the "problem" is, feel free to
clarify.
Hi,

for the last code snippet
this is the output i got

y
z
y
z

when i entered y , it didn't print anything
when i entered z, it printed both y and z.
What i expected is this,

y
y
z
z

Thanks for your time,
Yugi.

Sep 8 '06 #4
To solve this problem i modified the code as follows:

#include<stdio. h>
main(void)
{
char a;
flushall();
//scanf("%c\n",&a ); /* 1st scanf */
a=getchar();
printf("%c\n",a );
scanf("%c",&a); /* 2nd scanf */
printf("%c\n",a );
return 0;

}
but here i found more interesting part as, it does not stop for getting
input for 2nd scanf statement. Can someone , why??????????

Sep 8 '06 #5
"Shashank" <sm*******@gmai l.comwrites:
To solve this problem i modified the code as follows:

#include<stdio. h>
main(void)
{
char a;
flushall();
//scanf("%c\n",&a ); /* 1st scanf */
a=getchar();
printf("%c\n",a );
scanf("%c",&a); /* 2nd scanf */
printf("%c\n",a );
return 0;

}
What is flushall()? There is no such function in standard C. What is
it supposed to do?

--
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.
Sep 8 '06 #6
>
What is flushall()? There is no such function in standard C. What is
it supposed to do?
flushall(); flushes the buffer to aviod the entry of garbage values.

Sep 8 '06 #7
Shashank said:
>
>>
What is flushall()? There is no such function in standard C. What is
it supposed to do?

flushall(); flushes the buffer to aviod the entry of garbage values.
On your system, perhaps it does; but the C Standard does not guarantee this.
On some other system, it might do something else entirely.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 8 '06 #8
There is NO flushall() function.

There is one function fflush(), which flushes out the i/o stream data.

Richard Heathfield wrote:
Shashank said:
>
What is flushall()? There is no such function in standard C. What is
it supposed to do?
flushall(); flushes the buffer to aviod the entry of garbage values.

On your system, perhaps it does; but the C Standard does not guarantee this.
On some other system, it might do something else entirely.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 8 '06 #9
Ratan said:
There is NO flushall() function.
Correction: the C standard library does not provide one. Implementation
providers are free to provide a function by that name in a third-party
library if they choose, and at least one such provider does precisely that.
There is one function fflush(), which flushes out the i/o stream data.
Indeed - and fflush(NULL) flushes out /all/ data currently pending on
streams open for output or update. This is guaranteed by the Standard.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 8 '06 #10

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

Similar topics

4
7993
by: Intaek LIM | last post by:
Hello. I always used cin in C++ for standard input. But, with C, I must use scanf. Integers, chars are all fine with scanf. The problem is... I cannot read floating point value like 'double' with scanf. ----------------------- double d;
39
100837
by: Teh Charleh | last post by:
OK I have 2 similar programmes, why does the first one work and the second does not? Basically the problem is that the program seems to ignore the gets call if it comes after a scanf call. Please anything even a hint would be really helpful, I cant for the life of me see why the 2nd prog wont work... gets before scanf code:---------------------------------------------------------------------
12
9884
by: B Thomas | last post by:
Hi, I was reading O'Reilly's "Practical C programming" book and it warns against the use of scanf, suggesting to avoid using it completely . Instead it recomends to use using fgets and sscanf. However no explanation is offered other than that scanf handels end of lines very badly. I have exeperienced such problems when doing some numerical programming but never understood it. Things like some consequitive scanfs would not read in values...
5
3908
by: Eduardo Olivarez | last post by:
The following code does not work correctly on my machine. Either one of the scanf()'s alone work perfectly. However, when they are combined, the second scanf() call just reads what the first one received as imput, without taking any keyboard input and then the process terminates prematurely. I looked through the glibc documentation trying to figure out what was causing this to no avail, so I started messing around with the code. Strangely...
33
3201
by: Lalatendu Das | last post by:
Dear friends, I am getting a problem in the code while interacting with a nested Do-while loop It is skipping a scanf () function which it should not. I have written the whole code below. Please help me in finding why such thing is happening and what the remedy to it is. Kindly bear with my English. int main ()
6
26319
by: obdict | last post by:
Hello, I used scanf() in a while loop, which ensures that user input is valid (must be an integer no greater than 21 or less than 3). If user enters a number out of the range, or enters non-number, he/she will be asked to retry. /* start */ int n;
68
4686
by: stasgold | last post by:
Hello. I maybe reinvent the weel ... I'm trying to read positive integer number with the help of scanf, if the input value is not positive number but negaive one zero or char , i have to reread the input until I get the needed pos. number I wrote the code , but it doesn't work the way it should : if i put some char into input, the program goes infinite loop instead of promting me to enter a new value.
5
610
by: Jack | last post by:
Hi, can someone tell me what is wrong with this code. cout << "Enter the size of the array:"; cin >size; myVar = new char; myVar = 'a'; printf ("%c\n",myVar); puts ("Enter value for the first cell: ");
13
4404
by: FerrisUML | last post by:
Hello everyone! I new to C and am having the following problem. In the below program, the last scanf is being ignored and the program exits. Can anyone see anything that im doing wrong? Thanks in advance. #include <stdio.h> /* HOMEWORK: Assignment 1 Name: Dennis McQuilken
0
9879
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11349
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
10921
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...
0
9728
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8099
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
5940
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...
0
6142
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4776
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
4341
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.