Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Is fflush(stdin) is really required in C++ and is it good to do it this way to flush the newline in the buffer?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
704 views
Welcome To Ask or Share your Answers For Others

1 Answer

The call to fflush(stdin) is undefined behavior in C (and, consequetly, in C++).

The C language standard, ISO 9899:1999 states in 7.19.5.2/2

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

To discard the entire input up to the next end of line, in C++, the most robust approach is

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '
');

As for the program "flashing and going away", are you perhaps executing a console application on a Windows platform? Such applications are best executed from a console window (Start->run->cmd.exe)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...