c++为什么代码执行性后出现一个黑框(命令窗口)一闪而过解决办法

VS2012 输入代码执行后屏幕一闪而过不出现显示框:

#include <iostream>
int main ()
{
using namespace std;
const int ArSize = 20;
char name[ArSize];
char dessert[ArSize];

cout <<"Enter your name: ";
cin.getline(name,ArSize);
cout <<"Enter your favorite dessert: ";
cin.getline(dessert,ArSize);
cout <<"I have some delicious "<<dessert;
cout <<"for you,"<<name<<". ";
return 0;
}

没有出现黑框(命令行窗口)事因为执行完...cout <<"for you,"<<name<<". ";之后直接return 0,即执行完了,所以会关闭,所以可以在return之前加个system("pause");就可能观察输出情况了。改后程序如下:

#include <iostream>
int main ()
{
using namespace std;
const int ArSize = 20;
char name[ArSize];
char dessert[ArSize];

cout <<"Enter your name: ";
cin.getline(name,ArSize);
cout <<"Enter your favorite dessert: ";
cin.getline(dessert,ArSize);
cout <<"I have some delicious "<<dessert;
cout <<"for you,"<<name<<". ";
system("pause");
return 0;
}

原文地址:https://www.cnblogs.com/wangxiaochu/p/4428971.html