Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > fflush and stdin

Reply
Thread Tools

fflush and stdin

 
 
asit
Guest
Posts: n/a
 
      01-14-2008
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
 
Reply With Quote
 
 
 
 
jameskuyper@verizon.net
Guest
Posts: n/a
 
      01-14-2008
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".
 
Reply With Quote
 
 
 
 
asit
Guest
Posts: n/a
 
      01-14-2008
On Jan 14, 10:33 pm, (E-Mail Removed) 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.
 
Reply With Quote
 
jameskuyper@verizon.net
Guest
Posts: n/a
 
      01-14-2008
asit wrote:
> On Jan 14, 10:33 pm, (E-Mail Removed) 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?
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      01-14-2008
In article <(E-Mail Removed)>,
asit <(E-Mail Removed)> wrote:

>> 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
 
Reply With Quote
 
Randy Howard
Guest
Posts: n/a
 
      01-14-2008
On Mon, 14 Jan 2008 11:48:36 -0600, Richard Tobin wrote
(in article <fmg79k$47u$(E-Mail Removed)>):

> In article
> <(E-Mail Removed)>,
> asit <(E-Mail Removed)> wrote:
>
>>> 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


Unfortunately, on some platforms fflush(stdin) has implementation
defined behavior that does something sort of like what you might guess,
after you got past it not making sense.

The odds are his instructor either doesn't care about it not being
portable, or is not even aware it might be an issue outside of the
platform being used for the course. As such, the op should refer to
either his course notes or documentation for his platform to determine
if in fact his in one of those for which it can be used.


--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw





 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      01-14-2008
In addition to the other comments you've received wrt fflush(stdin),
please see below

On Jan 14, 12:13 pm, asit <(E-Mail Removed)> wrote:
> 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




 
Reply With Quote
 
asit
Guest
Posts: n/a
 
      01-14-2008
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;

}
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      01-14-2008
On Mon, 14 Jan 2008 10:09:14 -0800, Lew Pitcher wrote:
> On Jan 14, 12:13 pm, asit <(E-Mail Removed)> wrote:
>> 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.
 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      01-14-2008
On Jan 14, 1:14 pm, asit <(E-Mail Removed)> 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


No. The user will enter 1 or 2 /and/ at least one other character.
Think about it. Try it yourself. What keys do you hit in order to
enter the data to this prompt? Count them. Which keys are they? What
characters will scanf() see from those keys?

> scanf("%d",&i); //scanf successfully reads


If the user entered a number, then scanf() successfully reads that
number.
However, it is the data that the user entered after the number that /
this/ scanf does not read.


> switch(i)
> {
> case 1:
> printf("Enter any alphabet : ");
> fflush(stdin); //any unfetched data from i/p
> stream is cleared


Not according to the standard. Define "unfetched data". Define
"cleared". Now, go read the standard (which has already been quoted to
you) on how fflush() works.


> scanf("%c",&j); //now what's the problem(in
> GCC) ???


a) fflush(stdin) didn't do what you thought it would do.
b) there is still legitimate data in the buffer that /is not/ the data
that the user entered to your "Enter any alphabet" prompt.


> 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;
>
> }


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
fflush not flusing stdin! jaswinder C Programming 4 08-19-2010 01:09 PM
fflush(stdin), scanf and a space barry C Programming 5 06-09-2010 06:04 PM
STDIN, OUT, ERR and $stdin, out, err - Differences? Terry Cooper Ruby 7 06-09-2009 05:48 AM
fflush(stdin), scanf and a space Tagore C Programming 5 12-29-2008 10:43 PM
In fflush(stdin), what happens to flushed data? hugo27 C Programming 5 10-10-2004 04:59 PM



Advertisments