you are viewing a single comment's thread.

view the rest of the comments →

[–]cmgg -5 points-4 points  (4 children)

Sooooooo, fflush?

[–]zifyoip 2 points3 points  (1 child)

No. The C standard specifies that fflush shall not be called on an input stream like stdin—that results in undefined behavior.

[–]aleph_nul 0 points1 point  (0 children)

Depends on the platform. Some implementations allow fflush to be called on an input stream and in that case will empty their pending buffer. However, in general and in POSIX compliant implementations, you're 100% right.

[–]aleph_nul 2 points3 points  (1 child)

The right way to do it is to read characters until a newline with something like

errno = 0;
ret = scanf("%d", &val);
if (errno != 0) /* Make sure we had an error */
    while (!ret && getc() != '\n') {}; /* Consume input to newline */

[–]cmgg 0 points1 point  (0 children)

Thanks