Solution to C, C + + program execution Results Flash past method---system ("pause"), GetChar (), Cin.get ()

Source: Internet
Author: User

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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.