Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
while using scanf two different times..to remove enter from keyboard buffer ,when should we use fflush(stdin) and when is it not required..
for example:
scanf("%d",&a);
scanf("%d",&b);
is an fflush statement required between these two scanf...?
and suppose we use a tab or a space before the %d of the second scanf..will that always work? does that space compensate the enter key.?
Posted

No, why? The fflush function, as well as all "flush" methods are only for write-only or read/write streams/files, they write (commit) all unwritten data from the buffer to the file; this is related to buffering; in other words, after the call, the data written to the file is synchronized with the buffer. This is totally unrelated to read operations.

Your "does that space compensate the enter key?" is not clear. The space has nothing to do with "enter". Your code sample also does not imply any "enter". You press enter when you complete entering data. I must say that all "scan" methods of reading are pretty delicate; you need to have in your actual stream some data written exactly as expected by your format string(s); moreover, it's apparent that the result of any read operation depends on what was done by the previous scanf. Perhaps you need to experiment with it a bit and, if something is still unclear, describe what exactly you want to achieve, the structure of data to be achieved, or the sequence of the operations on entering data.

But I would give you one simple practical advice. I would not mess with scanf at all. We have other questions here on CodeProject, many people have been much confused, by a good reason. Instead, I would simply enter data by single lines: prompt-enter, prompt-enter… On each step, you get a whole line entered. You write prompts "Enter this and that", and call gets_s. The use enters the whole line and presses "Enter". Then you can parse the string obtained using sscanf_s. Handle the problems, and so on. I think such sequence is much easier to understand and maintain.

Please see:
http://en.cppreference.com/w/c/io/gets[^],
https://msdn.microsoft.com/en-us/library/t6z7bya3.aspx[^].

Also, be sure to use security-enhanced variants of all those functions. For example, don't use gets or scanf, use gets_s and scanf_s, and so on. Please see: https://msdn.microsoft.com/en-us/library/8ef0s5kh.aspx[^].

Now, some more on interactive input. Perhaps you really need to lean it all. But I would not take it too serious. If you look at existing really useful console-only applications, such as system utilities, they rarely use interactive input. The relatively rarely used class of such interactive console-only applications usually provides fully-fledged command interpreter behavior. All other applications are much simpler: they accept all the input at once, in the command line. If some portions of data are too big for writing in a command line, one or more parameters are file name, so the use adds more complicated data in a file and uses the file for entering data. The interactive input like yours is too inconvenient to the users; one mistake may cause the user to start over, and so on. Try not to get sunken in it.

—SA
 
Share this answer
 
v2
Comments
Member 11324568 21-Jan-15 12:55pm    
wow .thanks :)
Sergey Alexandrovich Kryukov 21-Jan-15 14:28pm    
You are very welcome.
Good luck, call again.
—SA
CPallini 21-Jan-15 13:27pm    
5.
Sergey Alexandrovich Kryukov 21-Jan-15 14:29pm    
Thank you, Carlo.
—SA
I have always found that scanf is not the best choice for reading input in C. I woud suggest using gets[^] and then using functions such as strtok and atoi to do the parsing and conversion. It is a bit more work but gives you (the programmer) better control.

[edit]
The use of fflush(stdin) is a useful feature to flush the input stream after a scanf, in order to remove any trailing whitespace and newline characters. This is obviously what I have been missing all these years.
[/edit]
 
Share this answer
 
v2
Comments
Member 11324568 21-Jan-15 12:44pm    
can you please give me example of where you found it causing error...because in the evaluation process i dont think the teaching assistants would accept anything other than scanf...im restricted to use that with fflush ..so i need to know prior to my labs when scanf doesnt work..thanks :)
Richard MacCutchan 21-Jan-15 13:07pm    
I did not say it caused an error, I said that I have not found it the best option. However, if you are taking a college course and forced to use it, then you should go to your teacher for help.
Member 11324568 21-Jan-15 13:18pm    
thanks..in solution 1 i mentioned a program which mentions error without flushing....and gets[^]is better option i have read about it now. :)
Richard MacCutchan 21-Jan-15 13:27pm    
Thanks for that comment, I learned something useful from you.
Member 11324568 21-Jan-15 13:29pm    
welcome ...its a fraction of what i had learnt from you till date :)
Executing fflush(stdin) invokes undefined behavior. It is out of the question. How did you even come up with the idea that it was a thing?

Contrary to other answers, I would say there is nothing wrong with using scanf, as long as you know exactly what every character in the format string is doing. In particular, %d does several things, the first of which is reading and discarding everything from the input stream as long as isspace() returns true (so it skips all spaces, tabs, and newlines).

That is exactly what makes it natural to enter two numbers and read them with

C#
scanf("%d",&a);
scanf("%d",&b);


If the numbers were entered on two separate lines, the second scanf reads and discards the newline left over by the first one. If they were entered on the same line, separated by spaces or tabs, the second scanf reads and discards those spaces or tabs.

(I would also check what those scanfs return, though)
 
Share this answer
 
v2
Comments
Richard MacCutchan 22-Jan-15 3:41am    
Not true; executing fflush(stdin) clears the input stream of any remaining whitespace, including newline characters, and leaves it ready for further input.
CubbiMew 22-Jan-15 5:59am    
No, where did you get this idea?
Richard MacCutchan 23-Jan-15 3:29am    
By reading the documentation, and actually testing it.
CubbiMew 23-Jan-15 6:33am    
Documentation for what exactly? If it claims to be documenting C, it is a lie. It is likely documenting some non-portable extension, like Microsoft's fflush, which is about as useful as clrscr.
Richard MacCutchan 23-Jan-15 6:52am    
it is a lie
Then I guess all of the following are lying; perhaps you should go and enlighten them that you know they are all doing it wrong. See http://man7.org/linux/man-pages/man3/fflush.3.html and http://www.cplusplus.com/reference/cstdio/fflush/ and https://msdn.microsoft.com/en-us/library/9yky46tz.aspx.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900