When you write a console program, including using other Ides (Visual C + +) to write a C + + program, you often see that the execution of the program is flashing through, to solve this problem, you can add system ("Pause"), GetChar (), Cin.get ().
The more common practice is to use System ("pause"), this article things to Avoid in C + +-system ("pause") does not recommend "system (" "Pause") because:
1, non-portable. For DOS or Windows only, not for Linux, etc.
2, consuming system resources. It's a bit overqualified to call system command systems () to do a "pause program."
3, must add the header file: Stdlib.h or Cstdlib
Therefore, should try to abandon.
Recommended method:
1, C, the use of GetChar ();
2, C + +, the use of cin.get ();
Enrich the two alternative approaches:
1, c
printf ("Press any key to continue ...");
GetChar ();
2. In C + +
cout<< "Press any key to continue ...";
Cin.clear ();
Cin.sync ();
Cin.get ();
Note: Add Cin.clear (), Cin.sync () These two sentences, is to empty the buffer, let Cin.get () really receive your keyboard input.
I note: sometimes only use cin.get () can not make the screen stop. Specific reasons:
Cin.get () is a character in the fetch stream
The cin>>sl; is entered with a carriage return and the carriage return remains in the buffer.
At this point when you cin.get (), because the buffer is not empty, directly read the carriage return
You can use Cin.sync () to empty the stream
Cin.get ();//Because you enter a carriage return when the string is output, and get () reads the carriage return, so you can't stop
When you add Fflush (stdin), the buffer carriage return Fu Qing except, so wait for your input and stop ~
Note: Technology lies in communication, communication, reproduced please specify the source and maintain the integrity of the work. ╰☆ struggle ing? Child ' original: http://blog.sina.com.cn/s/blog_a6fb6cc90101bg5f.html.
Solution to C, C + + program execution Results Flash past method---system ("pause"), GetChar (), Cin.get ()