473,224 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

fflush and stdin

please fix the bugs...??

#include <stdio.h>

int main()
{
int i;
char j;
printf("Enter any number ...(1 or 2) : ");
scanf("%d",&i);
switch(i)
{
case 1:
printf("Enter any alphabet : ");
fflush(stdin);
scanf("%c",&j);
switch(j)
{
case 'a':
printf("Winners never
quit...");
break;
case 'b':
printf("Quitters never
win...");
break;
}
break;
case 2:
printf("\nFailure is pillar of success..");
}
return 0;
}

It doesn't show the required o/p when run in GCC. Thank You
Jan 14 '08 #1
14 6183
asit wrote:
....
fflush(stdin);
7.19.5.2, descring fflush(stream), says:

"If stream points to an output stream or an update stream in which the
most recent operation was not input, the fflush function causes any
unwritten data for that stream to be delivered to the host environment
to be written to the file; otherwise, the behavior is undefined."

Now think carefully about "stdin" and the phrase "output stream".
Jan 14 '08 #2
On Jan 14, 10:33 pm, jameskuy...@verizon.net wrote:
asit wrote:

...
fflush(stdin);

7.19.5.2, descring fflush(stream), says:

"If stream points to an output stream or an update stream in which the
most recent operation was not input, the fflush function causes any
unwritten data for that stream to be delivered to the host environment
to be written to the file; otherwise, the behavior is undefined."

Now think carefully about "stdin" and the phrase "output stream".

still i can't fix it. please help me.
Jan 14 '08 #3
In article <61**********************************@v46g2000hsv. googlegroups.com>,
asit <li*****@gmail.comwrote:
>Now think carefully about "stdin" and the phrase "output stream".
>still i can't fix it. please help me.
What does fflush do? Look it up if you don't know. How might it be
relevant to your problem?

-- Richard
--
:wq
Jan 14 '08 #4
asit wrote:
On Jan 14, 10:33 pm, jameskuy...@verizon.net wrote:
asit wrote:

...
fflush(stdin);
7.19.5.2, describing fflush(stream), says:

"If stream points to an output stream or an update stream in which the
most recent operation was not input, the fflush function causes any
unwritten data for that stream to be delivered to the host environment
to be written to the file; otherwise, the behavior is undefined."

Now think carefully about "stdin" and the phrase "output stream".


still i can't fix it. please help me.
Think! It's not that difficult.

nFurther hints: does 'stdin' qualify as an "output stream"? What does
the above clause say about the results of using fflush() on a stream
which is not an "output stream"? Do you have an output stream in your
program? If so, what is it's name?
Jan 14 '08 #5
In addition to the other comments you've received wrt fflush(stdin),
please see below

On Jan 14, 12:13 pm, asit <lipu...@gmail.comwrote:
please fix the bugs...??

#include <stdio.h>

int main()
You aren't taking any arguments in your main(), so how should you have
declared main()? Remember, in a hosted environment, the only two
standard forms are
int main(int argc, char **argv)
and
int main(void)

{
int i;
char j;
printf("Enter any number ...(1 or 2) : ");
scanf("%d",&i);
What will happen if the user doesn't enter a number?
What will this scanf() /not/ read if the user /does/ enter a number?
switch(i)
{
case 1:
printf("Enter any alphabet : ");
fflush(stdin);
See other remarks wrt fflush(stdin)
scanf("%c",&j);
What will happen if the user does not enter a character? (Say, the
user causes end-of-input on stdin, and scanf() encounters the end-of-
file)

What will this scanf() read? Remember that you've previously picked
out /some/ data from the input stream, but the previous scanf() may
not have picked up everything/
switch(j)
{
case 'a':
printf("Winners never
quit...");
What happens when the printf() above executes? What will be printed?
What do you /think/ will be printed?
break;
case 'b':
printf("Quitters never
win...");
What happens when the printf() above executes? What will be printed?
What do you /think/ will be printed?
break;
}
break;
case 2:
printf("\nFailure is pillar of success..");
What happens when the printf() above executes? What will be printed?
What do you /think/ will be printed?
}
return 0;

}

It doesn't show the required o/p when run in GCC. Thank You


Jan 14 '08 #6
i have written the program considering user knows every constraint

#include <stdio.h>

int main()
{
int i;
char j;
printf("Enter any number ...(1 or 2) : "); //user will only
enter 1 or 2
scanf("%d",&i); //scanf successfully reads
switch(i)
{
case 1:
printf("Enter any alphabet : ");
fflush(stdin); //any unfetched data from i/p
stream is cleared
scanf("%c",&j); //now what's the problem(in
GCC) ???
switch(j)
{
case 'a':
printf("Winners never
quit...");
break;
case 'b':
printf("Quitters never
win...");
break;
}
break;
case 2:
printf("\nFailure is pillar of success..");
}
return 0;

}
Jan 14 '08 #7
On Mon, 14 Jan 2008 10:09:14 -0800, Lew Pitcher wrote:
On Jan 14, 12:13 pm, asit <lipu...@gmail.comwrote:
>please fix the bugs...??

#include <stdio.h>

int main()

You aren't taking any arguments in your main(), so how should you have
declared main()? Remember, in a hosted environment, the only two
standard forms are
int main(int argc, char **argv)
and
int main(void)
Or equivalent.

What
int main() { ... }
defines is equivalent to what
int main(void)
defines, even though as declarations, they have a different meaning.

Similarly, what
int main() int argc; char **argv; { ... }
defines is equivalent to what
int main(int argc, char **argv) { ... }
defines, even though again, the former has no prototype.

The standard itself uses the unprototyped int main() form in two
examples. While examples aren't normative, they do help clarify the
intent.
Jan 14 '08 #8
On Jan 14, 12:13 pm, asit <lipu...@gmail.comwrote:
please fix the bugs...??

#include <stdio.h>

int main()
int main(void)
{
int i;
char j;
printf("Enter any number ...(1 or 2) : ");
scanf("%d",&i);
scanf() with the "%d" conversion specifier isn't the greatest tool for
interactive user input. If the user fat-fingers a non-numeric
character, it will be left in the input stream (for example, if the
user types "1w3", scanf() will read and convert the "1", but leave
"w3" in the input stream). Instead of using scanf() for interactive
input, it's better to use fgets() and read everything as a string,
then convert as necessary with other tools. For one thing, it allows
you to recover from bad input more easily, and you can prevent stray
newlines from mucking things up later.
switch(i)
{
case 1:
printf("Enter any alphabet : ");
fflush(stdin);
And here's why you're not seeing what you expect. This doesn't do
what you think it does. Strictly speaking, calling fflush() on an
input stream results in undefined behavior; you cannot rely on it
clearing the input stream. The newline character from the prior
scanf() operation is still in the input stream, so that's the first
thing that the scanf below sees.
scanf("%c",&j);
switch(j)
{
case 'a':
printf("Winners never
quit...");
break;
case 'b':
printf("Quitters never
win...");
break;
}
break;
case 2:
printf("\nFailure is pillar of success..");
}
return 0;

}

It doesn't show the required o/p when run in GCC. Thank You
Jan 14 '08 #9
asit wrote:
please fix the bugs...??
No, but your subject line points us to these lines:
printf("Enter any alphabet : ");
fflush(stdin);
Which has one logical error and one instance of undefined behavior.
1) Failure to fflush(stdout) after the prompt means (as you discovered)
that input on stdin and output on stdout might not be properly synchronized.
2) fflush is not defined for output streams. It is defined for output
streams and for bidirectional streams on which the last operation was
output. stdin is an input stream and fflush(stdin) has no meaning.
It doesn't show the required o/p when run in GCC. Thank You
No kidding.
Jan 14 '08 #10
On Mon, 14 Jan 2008 10:14:08 -0800, asit wrote:
i have written the program considering user knows every constraint

#include <stdio.h>

int main()
{
int i;
char j;
printf("Enter any number ...(1 or 2) : "); //user will only
enter 1 or 2
If I press 1, nothing happens. I have to press something else, don't I,
if I want this to work? But doesn't that, also, end up in the input
buffer? How do you deal with it?
scanf("%d",&i); //scanf successfully reads switch(i)
It *might*. Or the user might accidentally hit something else, such as
'x'.
{
case 1:
printf("Enter any alphabet : ");
fflush(stdin); //any unfetched data from i/p
stream is cleared
Actually, fflush is only defined to work for _output_ streams. stdin is
an _input_ stream.
scanf("%c",&j); //now what's the problem(in
GCC) ???
If I had to guess, I'd say probably the stuff left over from the input of
the number gets read here and as a result, you get unexpected results.
printf("\nFailure is pillar of success..");
On another note, you should either end your outputs with a \n, or flush
the output buffer, as a rule, to ensure the printed text is actually seen.
Jan 14 '08 #11
asit wrote:
>
please fix the bugs...??

#include <stdio.h>

int main()
{
int i;
char j;
printf("Enter any number ...(1 or 2) : ");
scanf("%d",&i);
switch(i)
{
case 1:
printf("Enter any alphabet : ");
fflush(stdin);
scanf("%c",&j);
switch(j)
{
case 'a':
printf("Winners never
quit...");
break;
case 'b':
printf("Quitters never
win...");
break;
}
break;
case 2:
printf("\nFailure is pillar of success..");
}
return 0;
}

It doesn't show the required o/p when run in GCC. Thank You
/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

#define LENGTH 1
#define str(x) # x
#define xstr(x) str(x)

int main(void)
{
int rc;
char array[LENGTH + 1];
long i;

printf("Enter a number ...(1 or 2) : ");
fflush(stdout);
rc = fscanf(stdin, "%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getc(stdin);
}
if (rc == 0) {
array[0] = '\0';
}
if (rc == EOF) {
puts("rc == EOF");
exit(EXIT_FAILURE);
}
i = strtol(array, NULL, 10);
switch (i) {
case 1:
printf("Enter any alphabet : ");
fflush(stdout);
rc = fscanf(stdin, "%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getc(stdin);
}
if (rc == 0) {
array[0] = '\0';
}
if (rc == EOF) {
puts("rc == EOF");
exit(EXIT_FAILURE);
}
switch (array[0]) {
case 'a':
puts("Winners never quit...");
break;
default:
puts("Quitters never win...");
break;
}
break;
default:
puts("\nFailure is pillar of success..");
break;
}
return 0;
}

/* END new.c */
--
pete
Jan 14 '08 #12
Harald van Dijk <tr*****@gmail.comwrites:
On Mon, 14 Jan 2008 10:09:14 -0800, Lew Pitcher wrote:
[...]
>You aren't taking any arguments in your main(), so how should you have
declared main()? Remember, in a hosted environment, the only two
standard forms are
int main(int argc, char **argv)
and
int main(void)

Or equivalent.

What
int main() { ... }
defines is equivalent to what
int main(void)
defines, even though as declarations, they have a different meaning.
I remember a fairly subtle argument that the "int main()" form might
not be legal according to at least one plausible reading of the
standard. I've forgotten the details. In practice, I'd be very
surprised by an implementation that didn't allow both forms -- but
"int main(void)" is better style IMHO.
Similarly, what
int main() int argc; char **argv; { ... }
defines is equivalent to what
int main(int argc, char **argv) { ... }
defines, even though again, the former has no prototype.
I think you meant
int main(argc, argv) int argc; char **argv; { ... }
for the first one.
The standard itself uses the unprototyped int main() form in two
examples. While examples aren't normative, they do help clarify the
intent.
Interesting, I hadn't realized that. The examples are in 6.5.3.4p7
("The sizeof operator") and 6.7.5.3p20 ("Function declarators
(including prototypes)"). In both cases, the use of a non-prototyped
main() is not relevant to the example. I suspect both cases were just
unintentional oversights.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 15 '08 #13
On Mon, 14 Jan 2008 21:34:55 -0800, Keith Thompson wrote:
Harald van Dijk <tr*****@gmail.comwrites:
>Similarly, what
int main() int argc; char **argv; { ... }
defines is equivalent to what
int main(int argc, char **argv) { ... }
defines, even though again, the former has no prototype.

I think you meant
int main(argc, argv) int argc; char **argv; { ... }
for the first one.
Yes, of course, thank you.
Jan 15 '08 #14
asit wrote:
fflush(stdin); //any unfetched data from i/p
stream is cleared
Some implementations do define what fflush does on an input stream, but
the C standard doesn't, so that implementations can do whatever they like.
<ot>
For example, on my implementation it sets errno to EBADF and returns EOF.
</ot>

--
Army1987 (Replace "NOSPAM" with "email")
Jan 20 '08 #15

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

Similar topics

23
by: herrcho | last post by:
What's the difference between STDIN and Keyboard buffer ? when i get char through scanf, i type in some characters and press enter, then, where do the characters go ? to STDIN or Keyboard...
5
by: hugo27 | last post by:
hugo 27, Oct 9, 2004 Ref Docs: c.l.c FAQ article 12.26 . www.digitalmars.com sitemap.stdio.fflush Reading these Docs I understand that fflush does not summarily destroy or discard the...
6
by: ccdrbrg | last post by:
What is the best way to protect stdin within a library? I am writing a terminal based program that provides plugin capability using the dlopen() API. Sequencing program commands (typed) and...
5
by: baumann | last post by:
hi all, when fflush function must be called? and why? thanks baumann@pan
1
by: asdsd sir | last post by:
Hi!I'm new in Python and i'd like to ask some general questions about stdin,stdout... Firstly... if we type like something like : cat "file.txt"|python somefile.py #somefile.py import sys
5
by: Jack | last post by:
Why fflush in the following code does not work? #include <stdio.h> int main() { int n; char string; for ( n=0 ; n<12 ; n++ ) { printf( "Enter some words: " );
0
by: Chuck Gantz | last post by:
I have written two programs to test some stdin/stdout redirection ideas to eventually test a legacy console application writtn in C. One of the programs is a simple console app with some...
6
by: Rajesh S R | last post by:
Consider the following code: #include <stdio.h> int main( void ) { printf("Hello"); fflush(stdout); return 0; }
0
by: Gabriel Genellina | last post by:
En Thu, 25 Sep 2008 09:49:31 -0300, Almar Klein <almar.klein@gmail.com> escribi: Use subprocess.PIPE Usually the tricky part is to figure out exactly whether there is more input or not. With...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...

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.