如何立即停止c++控制台应用程序退出?

时间:2021-07-19 20:47:04

Lately, I've been trying to learn C++ from this website. Unfortunately whenever I try to run one of the code samples, I see that program open for about a half second and then immediately close. Is there a way to stop the program from closing immediately so that I can see the fruits of my effort?

最近,我一直在这个网站上学习c++。不幸的是,每当我尝试运行一个代码示例时,我都会看到该程序打开了大约半秒,然后立即关闭。有什么方法可以阻止程序立即关闭,让我看到我的努力的成果吗?

35 个解决方案

#1


98  

Edit: As Charles Bailey rightly points out in a comment below, this won't work if there are characters buffered in stdin, and there's really no good way to work around that. If you're running with a debugger attached, John Dibling's suggested solution is probably the cleanest solution to your problem.

编辑:正如Charles Bailey在下面的评论中指出的那样,如果在stdin中有缓冲字符,这将不会起作用,而且真的没有好的方法来解决这个问题。如果您正在使用附带的调试器运行,John Dibling的建议解决方案可能是您的问题的最干净的解决方案。

That said, I'll leave this here and maybe someone else will find it useful. I've used it a lot as a quick hack of sorts when writing tests during development.

也就是说,我把这个放在这里,也许其他人会觉得它有用。在开发期间编写测试时,我经常使用它作为一种快速的技巧。


At the end of your main function, you can call std::getchar();

在主函数的末尾,可以调用std::getchar();

This will get a single character from stdin, thus giving you the "press any key to continue" sort of behavior (if you actually want a "press any key" message, you'll have to print one yourself).

这将从stdin中获得一个字符,从而为您提供“按下任何键以继续”之类的行为(如果您确实想要“按任何键”消息,您必须自己打印一个)。

You need to #include <cstdio> for getchar.

您需要为getchar添加#include

#2


121  

If you are using Visual Studio and you are starting the console application out of the IDE:

如果您正在使用Visual Studio,并且您正在从IDE中启动控制台应用程序:

pressing CTRL-F5 (start without debugging) will start the application and keep the console window open until you press any key.

按CTRL-F5(无需调试即可启动)将启动应用程序并保持控制台窗口打开,直到您按下任何键为止。

#3


82  

The solution by James works for all Platforms.

James的解决方案适用于所有平台。

Alternatively on Windows you can also add the following just before you return from main function:

在Windows上,你也可以在从主函数返回之前添加以下内容:

  system("pause");

This will run the pause command which waits till you press a key and also displays a nice message Press any key to continue . . .

这将运行暂停命令,等待您按下一个键,并显示一个良好的消息按下任何键继续…

#4


63  

If you are using Microsoft's Visual C++ 2010 Express and run into the issue with CTRL+F5 not working for keeping the console open after the program has terminated, take a look at this MSDN thread.

如果您正在使用微软的Visual c++ 2010 Express,并遇到了CTRL+F5无法在程序终止后保持控制台打开的问题,请查看这个MSDN线程。

Likely your IDE is set to close the console after a CTRL+F5 run; in fact, an "Empty Project" in Visual C++ 2010 closes the console by default. To change this, do as the Microsoft Moderator suggested:

可能您的IDE将在CTRL+F5运行后关闭控制台;事实上,Visual c++ 2010中的“空项目”默认关闭控制台。要改变这一点,请按照微软版主的建议:

Please right click your project name and go to Properties page, please expand Configuration Properties -> Linker -> System, please select Console (/SUBSYSTEM:CONSOLE) in SubSystem dropdown. Because, by default, the Empty project does not specify it.

请右键点击您的项目名称,进入属性页面,请展开配置属性—>链接器—>系统,请在子系统下拉菜单中选择控制台(/子系统:控制台)。因为默认情况下,空项目不会指定它。

#5


17  

I usually just put a breakpoint on main()'s closing curly brace. When the end of the program is reached by whatever means the breakpoint will hit and you can ALT-Tab to the console window to view the output.

我通常只是在main()的右大括号上加上断点。当程序结束时,断点将会被点击,并且您可以通过ALT-Tab切换到控制台窗口以查看输出。

#6


13  

Why not just run the program from a console ie run the program from cmd.exe if you're using Windows. That way the window stays open after the program finishes.

为什么不直接从控制台运行程序呢?即从cmd运行程序。如果你使用的是Windows系统。这样,程序结束后窗口仍然保持打开状态。

[EDIT]: When I use KDevelop4 there is a fully fledged instance of Bash (a Linux CLI) running in a tab at the bottom of the IDE. Which is what I use in these sort of circumstances.

[编辑]:当我使用KDevelop4时,有一个完整的Bash实例(一个Linux CLI)在IDE底部的一个选项卡中运行。这就是我在这种情况下使用的。

#7


7  

Before the end of your code, insert this line:

在您的代码结束之前,插入这一行:

system("pause");

This will keep the console until you hit a key.

这将保持控制台直到您点击一个键。

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s;
    cout << "Please enter your first name followed by a newline\n";
    cin >> s;
    cout << "Hello, " << s << '\n';
    system("pause"); // <----------------------------------
    return 0; // This return statement isn't necessary
}

#8


6  

Call cin.get(); 2 times:

调用cin.get();2次:

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

#9


4  

If you run your code from a competent IDE, such as Code::Blocks, the IDE will manage the console it uses to run the code, keeping it open when the application closes. You don't want to add special code to keep the console open, because this will prevent it functioning correctly when you use it for real, outside of the IDE.

如果您从一个合适的IDE(例如::Blocks)运行代码,IDE将管理它用来运行代码的控制台,在应用程序关闭时保持它的打开状态。您不希望添加特殊的代码来保持控制台的打开,因为当您在IDE之外真正地使用它时,这会阻止它正确地运行。

#10


3  

Okay I'm guessing you are on Windows using Visual Studio... why? Well because if you are on some sort of Linux OS then you'd probably be running it from the console.

好吧,我猜你是在Windows上使用Visual Studio…为什么?因为如果你在某种Linux操作系统上,你可能会在控制台运行它。

Anyways, you can add crap to the end of your program like others are suggesting, or you can just hit CTRL + F5 (start without debugging) and Visual Studio will leave the console up once complete.

不管怎样,你可以像其他人建议的那样在程序的末尾添加一些垃圾,或者你可以按CTRL + F5(不用调试就可以开始),Visual Studio一旦完成控制台就会离开。

Another option if you want to run the Debug version and not add crap to your code is to open the console window (Start -> Run -> cmd) and navigate to your Debug output directory. Then, just enter the name of your executable and it will run your debug program in the console. You can then use Visual Studio's attach to process or something if you really want to.

如果您希望运行调试版本而不向代码中添加垃圾,另一个选项是打开控制台窗口(Start -> run -> cmd)并导航到您的调试输出目录。然后,只需输入可执行文件的名称,它就会在控制台中运行您的调试程序。然后你可以使用Visual Studio的attach to process或者其他你想要的东西。

#11


2  

If you are actually debugging your application in Visual C++, press F5 or the green triangle on the toolbar. If you aren't really debugging it (you have no breakpoints set), press Ctrl+F5 or choose Start Without Debugging on the menus (it's usually on the Debug menu, which I agree is confusing.) It will be a little faster, and more importantly to you, will pause at the end without you having to change your code.

如果您正在用Visual c++调试应用程序,请按F5或工具栏上的绿色三角形。如果您没有真正调试它(您没有设置断点),按Ctrl+F5或在菜单上选择Start(通常在调试菜单上,我同意这是令人困惑的)。它会更快一点,对您来说更重要的是,它会在结束时暂停而不必更改代码。

Alternatively, open a command prompt, navigate to the folder where your exe is, and run it by typing its name. That way when it's finished running the command prompt doesn't close and you can see the output. I prefer both of these methods to adding code that stops the app just as its finished.

或者,打开一个命令提示符,导航到exe所在的文件夹,并输入它的名称来运行它。这样,当它完成运行时,命令提示符不会关闭,您可以看到输出。我更喜欢这两种方法,而不是添加代码,使应用程序在完成时停止。

#12


2  

Just add the following at the end of your program. It will try to capture some form of user input thus it stops the console from closing automatically.

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

cin.get();

#13


2  

Add the following lines before any exit() function or before any returns in main():

在任何exit()函数或main()中的任何返回之前添加以下行:

std::cout << "Paused, press ENTER to continue." << std::endl;
cin.ignore(100000, "\n");

#14


1  

For Visual Studio (and only Visual Studio) the following code snippet gives you a 'wait for keypress to continue' prompt that truly waits for the user to press a new key explicitly, by first flushing the input buffer:

对于Visual Studio(仅为Visual Studio),下面的代码片段提供了一个“等待按键继续”提示符,通过首先刷新输入缓冲区,真正地等待用户显式地按下一个新键:

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

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

Note that this uses the tchar.h macro's to be compatible with multiple 'character sets' (as VC++ calls them).

注意,这里使用的是tchar。h宏将与多个“字符集”兼容(如vc++所称)。

#15


1  

Use #include "stdafx.h" & system("pause"); just like the code down below.

使用“stdafx # include。h”&系统(“暂停”);就像下面的代码一样。

#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");
}

#16


1  

Similar idea to yeh answer, just minimalist alternative.

类似的想法你回答,只是极简的选择。

Create a batch file with the following content:

创建具有以下内容的批处理文件:

helloworld.exe
pause

Then use the batch file.

然后使用批处理文件。

#17


1  

#include "stdafx.h"
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{

    cout << "Press any key to continue...";
    getch();

    return 0;
}

I didnt notice anyone else post this so here it is.

我没注意到有人贴了这个,在这儿。

#18


0  

See if your IDE has a checkbox in project setting to keep the window open after the program terminates. If not, use std::cin.get(); to read a character at the end of main function. However, be sure to use only line-based input (std::getline) or to deal with leftover unread characters otherwise (std::ignore until newline) because otherwise the .get() at the end will only read the garbage you left unread earlier.

查看您的IDE在项目设置中是否有一个复选框,以便在程序终止后保持窗口打开。如果没有,使用std::cin.get();在主函数的末尾读取字符。但是,请确保只使用基于行的输入(std: getline)或处理未读字符,否则(std:::忽略直到换行),否则结尾的.get()将只读取未读的垃圾。

#19


0  

This seems to work well:

这似乎很有效:

cin.clear();
cin.ignore(2);

If you clear the buffer first it won't be a problem when you read the next one. For some reason cin.ignore(1) does not work, it has to be 2.

如果您先清除缓冲区,当您读取下一个缓冲区时,它不会成为一个问题。出于某种原因,忽略(1)不起作用,它必须是2。

#20


0  

All you have to do set a variable for x then just type this in before the return 0;

你只需要为x设置一个变量然后在返回0之前输入这个;

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

#21


0  

You can even declare an integer at the beginning of your main() function (say int a;) and put std::cin >> a; just before the return value. So the program will keep running until you press a key and enter.

您甚至可以在main()函数的开头声明一个整数(比如int a;),并将std::cin >> a;在返回值之前。所以程序将继续运行,直到你按下一个键并输入。

#22


0  

You could always just create a batch file. For example, if your program is called helloworld.exe, some code would be:

您可以创建一个批处理文件。例如,如果您的程序被称为helloworld。exe,一些代码是:

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

#23


0  

I'm putting a breakpoint at the last return 0 of the program. It works fine.

我在程序的最后一个返回0处放置一个断点。它将正常工作。

#24


0  

simply

简单的

int main(){
    // code...
    getchar();
    getchar();
    return 0;
}

#25


0  

I used cin.get() and that is worked but one day I needed to use another cin.get([Array Variable]) before that to grab a ling string with blank character in middle of. so the cin.get() didn't avoid command prompt window from closing. Finally I found Another way: Press CTRL+F5 to open in an external window and Visual Studio does not have control over it anymore. Just will ask you about closing after final commands run.

我使用了cin.get(),这是可行的,但是有一天我需要使用另一个cin。获取([Array Variable]),在此之前获取中间为空字符的ling字符串。所以cin.get()并没有避免命令提示窗口关闭。最后我找到了另一种方法:按CTRL+F5打开外部窗口,Visual Studio不再控制它。在最后的命令运行之后,我们会问你关于关闭的问题。

#26


0  

I just do this:

我只是这样做:

//clear buffer, wait for input to close program
std::cin.clear(); std::cin.ignore(INT_MAX, '\n');
std::cin.get();
return 0;

Note: clearing the cin buffer and such is only necessary if you've used cin at some point earlier in your program. Also using std::numeric_limits::max() is probably better then INT_MAX, but it's a bit wordy and usually unnecessary.

注意:清除cin缓冲区,只有在您在程序早期使用过cin时才需要这样做。同样使用std:::numeric_limits: max()可能比INT_MAX要好,但它有点啰嗦,通常没有必要。

#27


0  

I tried putting a getchar() function at the end. But it didn't work. So what I did was add two getchar() functions one after another. I think the first getchar() absorbs the Enter key you press after the last data input. So try adding two getchar() functions instead of one

我尝试在末尾添加getchar()函数。但它不工作。我所做的就是一个接一个地添加两个getchar()函数。我认为第一个getchar()吸收了在最后一个数据输入之后按下的Enter键。因此,尝试添加两个getchar()函数而不是一个

#28


0  

Instead of pressing the run button, press CTRL and F5 at the same time, it will give you the press any key to continue message. Or type "(warning use this only for testing not actual programs as an antiviruses don't like it!!!!)" at the end of your main function but: (warning use this only for testing not actual programs as an antiviruses don't like it!!!!)

不是按run按钮,同时按CTRL和F5,它会给你按任意键继续消息。或者在你的主要功能的末尾输入“”(警告使用这个只用于测试不是真正的程序,因为病毒不喜欢它!!!)

#29


0  

just use cin.ignore() right before return 0; twice

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

main()
  {
  //your codes 

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

  return 0;
  }

thats all

这是所有

#30


-1  

If you are running Windows, then you can do system("pause >nul"); or system("pause");. It executes a console command to pause the program until you press a key. >nul prevents it from saying Press any key to continue....

如果您正在运行Windows,那么您可以执行系统(“暂停>nul”);或系统(“暂停”);。它执行控制台命令来暂停程序,直到您按下一个键。> nul阻止它说按任意键继续....

#1


98  

Edit: As Charles Bailey rightly points out in a comment below, this won't work if there are characters buffered in stdin, and there's really no good way to work around that. If you're running with a debugger attached, John Dibling's suggested solution is probably the cleanest solution to your problem.

编辑:正如Charles Bailey在下面的评论中指出的那样,如果在stdin中有缓冲字符,这将不会起作用,而且真的没有好的方法来解决这个问题。如果您正在使用附带的调试器运行,John Dibling的建议解决方案可能是您的问题的最干净的解决方案。

That said, I'll leave this here and maybe someone else will find it useful. I've used it a lot as a quick hack of sorts when writing tests during development.

也就是说,我把这个放在这里,也许其他人会觉得它有用。在开发期间编写测试时,我经常使用它作为一种快速的技巧。


At the end of your main function, you can call std::getchar();

在主函数的末尾,可以调用std::getchar();

This will get a single character from stdin, thus giving you the "press any key to continue" sort of behavior (if you actually want a "press any key" message, you'll have to print one yourself).

这将从stdin中获得一个字符,从而为您提供“按下任何键以继续”之类的行为(如果您确实想要“按任何键”消息,您必须自己打印一个)。

You need to #include <cstdio> for getchar.

您需要为getchar添加#include

#2


121  

If you are using Visual Studio and you are starting the console application out of the IDE:

如果您正在使用Visual Studio,并且您正在从IDE中启动控制台应用程序:

pressing CTRL-F5 (start without debugging) will start the application and keep the console window open until you press any key.

按CTRL-F5(无需调试即可启动)将启动应用程序并保持控制台窗口打开,直到您按下任何键为止。

#3


82  

The solution by James works for all Platforms.

James的解决方案适用于所有平台。

Alternatively on Windows you can also add the following just before you return from main function:

在Windows上,你也可以在从主函数返回之前添加以下内容:

  system("pause");

This will run the pause command which waits till you press a key and also displays a nice message Press any key to continue . . .

这将运行暂停命令,等待您按下一个键,并显示一个良好的消息按下任何键继续…

#4


63  

If you are using Microsoft's Visual C++ 2010 Express and run into the issue with CTRL+F5 not working for keeping the console open after the program has terminated, take a look at this MSDN thread.

如果您正在使用微软的Visual c++ 2010 Express,并遇到了CTRL+F5无法在程序终止后保持控制台打开的问题,请查看这个MSDN线程。

Likely your IDE is set to close the console after a CTRL+F5 run; in fact, an "Empty Project" in Visual C++ 2010 closes the console by default. To change this, do as the Microsoft Moderator suggested:

可能您的IDE将在CTRL+F5运行后关闭控制台;事实上,Visual c++ 2010中的“空项目”默认关闭控制台。要改变这一点,请按照微软版主的建议:

Please right click your project name and go to Properties page, please expand Configuration Properties -> Linker -> System, please select Console (/SUBSYSTEM:CONSOLE) in SubSystem dropdown. Because, by default, the Empty project does not specify it.

请右键点击您的项目名称,进入属性页面,请展开配置属性—>链接器—>系统,请在子系统下拉菜单中选择控制台(/子系统:控制台)。因为默认情况下,空项目不会指定它。

#5


17  

I usually just put a breakpoint on main()'s closing curly brace. When the end of the program is reached by whatever means the breakpoint will hit and you can ALT-Tab to the console window to view the output.

我通常只是在main()的右大括号上加上断点。当程序结束时,断点将会被点击,并且您可以通过ALT-Tab切换到控制台窗口以查看输出。

#6


13  

Why not just run the program from a console ie run the program from cmd.exe if you're using Windows. That way the window stays open after the program finishes.

为什么不直接从控制台运行程序呢?即从cmd运行程序。如果你使用的是Windows系统。这样,程序结束后窗口仍然保持打开状态。

[EDIT]: When I use KDevelop4 there is a fully fledged instance of Bash (a Linux CLI) running in a tab at the bottom of the IDE. Which is what I use in these sort of circumstances.

[编辑]:当我使用KDevelop4时,有一个完整的Bash实例(一个Linux CLI)在IDE底部的一个选项卡中运行。这就是我在这种情况下使用的。

#7


7  

Before the end of your code, insert this line:

在您的代码结束之前,插入这一行:

system("pause");

This will keep the console until you hit a key.

这将保持控制台直到您点击一个键。

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s;
    cout << "Please enter your first name followed by a newline\n";
    cin >> s;
    cout << "Hello, " << s << '\n';
    system("pause"); // <----------------------------------
    return 0; // This return statement isn't necessary
}

#8


6  

Call cin.get(); 2 times:

调用cin.get();2次:

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

#9


4  

If you run your code from a competent IDE, such as Code::Blocks, the IDE will manage the console it uses to run the code, keeping it open when the application closes. You don't want to add special code to keep the console open, because this will prevent it functioning correctly when you use it for real, outside of the IDE.

如果您从一个合适的IDE(例如::Blocks)运行代码,IDE将管理它用来运行代码的控制台,在应用程序关闭时保持它的打开状态。您不希望添加特殊的代码来保持控制台的打开,因为当您在IDE之外真正地使用它时,这会阻止它正确地运行。

#10


3  

Okay I'm guessing you are on Windows using Visual Studio... why? Well because if you are on some sort of Linux OS then you'd probably be running it from the console.

好吧,我猜你是在Windows上使用Visual Studio…为什么?因为如果你在某种Linux操作系统上,你可能会在控制台运行它。

Anyways, you can add crap to the end of your program like others are suggesting, or you can just hit CTRL + F5 (start without debugging) and Visual Studio will leave the console up once complete.

不管怎样,你可以像其他人建议的那样在程序的末尾添加一些垃圾,或者你可以按CTRL + F5(不用调试就可以开始),Visual Studio一旦完成控制台就会离开。

Another option if you want to run the Debug version and not add crap to your code is to open the console window (Start -> Run -> cmd) and navigate to your Debug output directory. Then, just enter the name of your executable and it will run your debug program in the console. You can then use Visual Studio's attach to process or something if you really want to.

如果您希望运行调试版本而不向代码中添加垃圾,另一个选项是打开控制台窗口(Start -> run -> cmd)并导航到您的调试输出目录。然后,只需输入可执行文件的名称,它就会在控制台中运行您的调试程序。然后你可以使用Visual Studio的attach to process或者其他你想要的东西。

#11


2  

If you are actually debugging your application in Visual C++, press F5 or the green triangle on the toolbar. If you aren't really debugging it (you have no breakpoints set), press Ctrl+F5 or choose Start Without Debugging on the menus (it's usually on the Debug menu, which I agree is confusing.) It will be a little faster, and more importantly to you, will pause at the end without you having to change your code.

如果您正在用Visual c++调试应用程序,请按F5或工具栏上的绿色三角形。如果您没有真正调试它(您没有设置断点),按Ctrl+F5或在菜单上选择Start(通常在调试菜单上,我同意这是令人困惑的)。它会更快一点,对您来说更重要的是,它会在结束时暂停而不必更改代码。

Alternatively, open a command prompt, navigate to the folder where your exe is, and run it by typing its name. That way when it's finished running the command prompt doesn't close and you can see the output. I prefer both of these methods to adding code that stops the app just as its finished.

或者,打开一个命令提示符,导航到exe所在的文件夹,并输入它的名称来运行它。这样,当它完成运行时,命令提示符不会关闭,您可以看到输出。我更喜欢这两种方法,而不是添加代码,使应用程序在完成时停止。

#12


2  

Just add the following at the end of your program. It will try to capture some form of user input thus it stops the console from closing automatically.

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

cin.get();

#13


2  

Add the following lines before any exit() function or before any returns in main():

在任何exit()函数或main()中的任何返回之前添加以下行:

std::cout << "Paused, press ENTER to continue." << std::endl;
cin.ignore(100000, "\n");

#14


1  

For Visual Studio (and only Visual Studio) the following code snippet gives you a 'wait for keypress to continue' prompt that truly waits for the user to press a new key explicitly, by first flushing the input buffer:

对于Visual Studio(仅为Visual Studio),下面的代码片段提供了一个“等待按键继续”提示符,通过首先刷新输入缓冲区,真正地等待用户显式地按下一个新键:

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

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

Note that this uses the tchar.h macro's to be compatible with multiple 'character sets' (as VC++ calls them).

注意,这里使用的是tchar。h宏将与多个“字符集”兼容(如vc++所称)。

#15


1  

Use #include "stdafx.h" & system("pause"); just like the code down below.

使用“stdafx # include。h”&系统(“暂停”);就像下面的代码一样。

#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");
}

#16


1  

Similar idea to yeh answer, just minimalist alternative.

类似的想法你回答,只是极简的选择。

Create a batch file with the following content:

创建具有以下内容的批处理文件:

helloworld.exe
pause

Then use the batch file.

然后使用批处理文件。

#17


1  

#include "stdafx.h"
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{

    cout << "Press any key to continue...";
    getch();

    return 0;
}

I didnt notice anyone else post this so here it is.

我没注意到有人贴了这个,在这儿。

#18


0  

See if your IDE has a checkbox in project setting to keep the window open after the program terminates. If not, use std::cin.get(); to read a character at the end of main function. However, be sure to use only line-based input (std::getline) or to deal with leftover unread characters otherwise (std::ignore until newline) because otherwise the .get() at the end will only read the garbage you left unread earlier.

查看您的IDE在项目设置中是否有一个复选框,以便在程序终止后保持窗口打开。如果没有,使用std::cin.get();在主函数的末尾读取字符。但是,请确保只使用基于行的输入(std: getline)或处理未读字符,否则(std:::忽略直到换行),否则结尾的.get()将只读取未读的垃圾。

#19


0  

This seems to work well:

这似乎很有效:

cin.clear();
cin.ignore(2);

If you clear the buffer first it won't be a problem when you read the next one. For some reason cin.ignore(1) does not work, it has to be 2.

如果您先清除缓冲区,当您读取下一个缓冲区时,它不会成为一个问题。出于某种原因,忽略(1)不起作用,它必须是2。

#20


0  

All you have to do set a variable for x then just type this in before the return 0;

你只需要为x设置一个变量然后在返回0之前输入这个;

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

#21


0  

You can even declare an integer at the beginning of your main() function (say int a;) and put std::cin >> a; just before the return value. So the program will keep running until you press a key and enter.

您甚至可以在main()函数的开头声明一个整数(比如int a;),并将std::cin >> a;在返回值之前。所以程序将继续运行,直到你按下一个键并输入。

#22


0  

You could always just create a batch file. For example, if your program is called helloworld.exe, some code would be:

您可以创建一个批处理文件。例如,如果您的程序被称为helloworld。exe,一些代码是:

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

#23


0  

I'm putting a breakpoint at the last return 0 of the program. It works fine.

我在程序的最后一个返回0处放置一个断点。它将正常工作。

#24


0  

simply

简单的

int main(){
    // code...
    getchar();
    getchar();
    return 0;
}

#25


0  

I used cin.get() and that is worked but one day I needed to use another cin.get([Array Variable]) before that to grab a ling string with blank character in middle of. so the cin.get() didn't avoid command prompt window from closing. Finally I found Another way: Press CTRL+F5 to open in an external window and Visual Studio does not have control over it anymore. Just will ask you about closing after final commands run.

我使用了cin.get(),这是可行的,但是有一天我需要使用另一个cin。获取([Array Variable]),在此之前获取中间为空字符的ling字符串。所以cin.get()并没有避免命令提示窗口关闭。最后我找到了另一种方法:按CTRL+F5打开外部窗口,Visual Studio不再控制它。在最后的命令运行之后,我们会问你关于关闭的问题。

#26


0  

I just do this:

我只是这样做:

//clear buffer, wait for input to close program
std::cin.clear(); std::cin.ignore(INT_MAX, '\n');
std::cin.get();
return 0;

Note: clearing the cin buffer and such is only necessary if you've used cin at some point earlier in your program. Also using std::numeric_limits::max() is probably better then INT_MAX, but it's a bit wordy and usually unnecessary.

注意:清除cin缓冲区,只有在您在程序早期使用过cin时才需要这样做。同样使用std:::numeric_limits: max()可能比INT_MAX要好,但它有点啰嗦,通常没有必要。

#27


0  

I tried putting a getchar() function at the end. But it didn't work. So what I did was add two getchar() functions one after another. I think the first getchar() absorbs the Enter key you press after the last data input. So try adding two getchar() functions instead of one

我尝试在末尾添加getchar()函数。但它不工作。我所做的就是一个接一个地添加两个getchar()函数。我认为第一个getchar()吸收了在最后一个数据输入之后按下的Enter键。因此,尝试添加两个getchar()函数而不是一个

#28


0  

Instead of pressing the run button, press CTRL and F5 at the same time, it will give you the press any key to continue message. Or type "(warning use this only for testing not actual programs as an antiviruses don't like it!!!!)" at the end of your main function but: (warning use this only for testing not actual programs as an antiviruses don't like it!!!!)

不是按run按钮,同时按CTRL和F5,它会给你按任意键继续消息。或者在你的主要功能的末尾输入“”(警告使用这个只用于测试不是真正的程序,因为病毒不喜欢它!!!)

#29


0  

just use cin.ignore() right before return 0; twice

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

main()
  {
  //your codes 

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

  return 0;
  }

thats all

这是所有

#30


-1  

If you are running Windows, then you can do system("pause >nul"); or system("pause");. It executes a console command to pause the program until you press a key. >nul prevents it from saying Press any key to continue....

如果您正在运行Windows,那么您可以执行系统(“暂停>nul”);或系统(“暂停”);。它执行控制台命令来暂停程序,直到您按下一个键。> nul阻止它说按任意键继续....