最近,我一直试图从这个网站学习c++。不幸的是,每当我试图运行其中一个代码示例时,我看到该程序打开了大约半秒钟,然后立即关闭。有没有办法阻止程序立即关闭,以便我能看到我的努力的成果?


当前回答

包括conio.h,在代码的末尾,在返回0之前,写入_getch();

其他回答

使用#include "stdafx.h" & system("pause");就像下面的代码一样。

#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
    std::cout << "hello programmer!\n\nEnter 2 numbers: ";
    int x, y;
    std::cin >> x >> y;
    int w = x*y;
    std::cout <<"\nyour answer is: "<< w << endl;
    system("pause");
}

我通常只在main()的右大括号上加一个断点。当程序以任何方式结束时,断点将出现,您可以ALT-Tab到控制台窗口以查看输出。

你也可以试着这样做

sleep (50000);
cout << "any text" << endl;

这将保存您的代码50000m,然后打印消息并关闭。但请记住,它不会永远暂停。

只需在程序的末尾添加以下内容。它将尝试捕获某种形式的用户输入,从而阻止控制台自动关闭。

cin.get();

你所要做的就是为x设置一个变量,然后在返回0之前输入这个;

cout<<"\nPress any key and hit enter to end...";
cin>>x;