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


当前回答

我在程序的最后一个返回0处设置了断点。它工作得很好。

其他回答

对于Visual Studio(并且只有Visual Studio),下面的代码片段给了你一个'wait For keypress to continue'提示,它真正地等待用户显式地按下一个新键,首先刷新输入缓冲区:

#include <cstdio>
#include <tchar.h>
#include <conio.h>

_tprintf(_T("Press a key to continue "));
while( _kbhit() /* defined in conio.h */ ) _gettch();
_gettch();

注意,这里使用了tchar.h宏来兼容多个“字符集”(vc++称之为字符集)。

只需在返回0之前使用cin.ignore();两次

main()
  {
  //your codes 

  cin.ignore();
  cin.ignore();

  return 0;
  }

thats所有

你可以只创建一个批处理文件。例如,如果你的程序名为helloworld.exe,一些代码将是:

@echo off
:1
cls
call helloworld.exe
pause >nul
goto :1

我试着在最后放一个getchar()函数。但这并没有起作用。所以我所做的就是一个接一个地添加两个getchar()函数。我认为第一个getchar()吸收了您在最后一个数据输入后按下的Enter键。所以尝试添加两个getchar()函数而不是一个

调用cin.get ();2次:

    //...
    cin.get();
    cin.get();
    return 0
}