1

Okay , i was solving a problem in code chef (very easy). It briefly states that :

-A question as a string will be given , and another string has to be produced which does not have any letter used in question string. Uppercase and lowercase are assumed to be same.

-If all alphabets have been used just print ~ sign.

My whole logic is correct except i caught error in my input and this was caused by using fflush(stdin) before gets(). Please explain why was this an error ? Ignore the rest of the code.

Link to problem :http://www.codechef.com/problems/NOLOGIC/

Link to wrong solution :http://www.codechef.com/viewsolution/3881817

Link to corrected solution :http://www.codechef.com/viewsolution/3881827

Pls Note :In corrected code i have only made change in not using fflush(stdin) and using getchar()

Number945
  • 4,631
  • 8
  • 45
  • 83
  • 6
    `fflush` is meant to be used on output streams, not input streams. http://stackoverflow.com/questions/2979209/using-fflushstdin/2979217#2979217 – R Sahu May 10 '14 at 04:48
  • 2
    fflush(stdin) is undefined behavior – Martin G May 10 '14 at 04:53
  • @RSahu & scy7he, it depends on the implementation. For example, on Linux: "For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application. The open status of the stream is unaffected." [ http://linux.die.net/man/3/fflush ] – Mahonri Moriancumer May 10 '14 at 05:50
  • Breaking Benjamin, please edit your question and elaborate about the development and execution environment where the error was encountered. Specifically, what is the target OS (and perhaps your compiler)? – Mahonri Moriancumer May 10 '14 at 05:54
  • I don't know about environment code chef uses . However i am using mingw with code blocks on win 7 and my incorrect solution was working fine. – Number945 May 10 '14 at 06:06
  • 1
    @BreakingBenjamin "undefined behavior" means that it could work or it could not work, or maybe it only works on fridays. Read the first link posted by RSahu – Brandin May 10 '14 at 07:42

1 Answers1

2

Being that the behavior of fflush(stdin) is implementation specific; and being that the implementation (for this question) is unknown, the behavior will also be unknown.

You may safely expect the behavior of fflush(stdin) to be unpredictable.

Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28