468,512 Members | 1,346 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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 5737
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by ccdrbrg | last post: by
5 posts views Thread by baumann | last post: by
5 posts views Thread by Jack | last post: by
reply views Thread by Chuck Gantz | last post: by
6 posts views Thread by Rajesh S R | last post: by
reply views Thread by Gabriel Genellina | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.