[C++] 用C++实现“按任意键继续”

很久没有写过C++程序了,对Visual Studio有些不习惯。尤其对它编译后直接退出感到无语到愤怒。。。

  http://blog.sina.com.cn/blogagn

于是就谷歌访问了一下,发现有几下方法:
1.直接调用系统函数 system("pause");
例如:
#include<iostream>
using namespace std;
int main()
{
    system("pause");
    return 0;
}
2.调用getch()函数:需要include<conio.h>
例如:
#include<conio.h>
int main()
{
    prinf("按任意键继续\n");
    getch();
    return 0;
}
3.调用getchar()函数:需要include<stdio.h>
例如:
#include<stdio.h>
int main()
{
    prinf("按 Enter 键继续\n");
    getchar();
    return 0;
}
4.使用cin的get()函数
例如:
#include<iostream>
using namespace std;
int main()
{
    cout<<"按 Enter 键继续"<<endl;
    cin.get();
    return 0;
}
注意:只有前两种可以真正实现“按任意键继续”,后两种必需按下Enter 键才行。
原文地址:https://www.cnblogs.com/robbychan/p/3787009.html