简体   繁体   English

如何制作打印文本并使其保留在 window 中的代码?

[英]How to make a code that prints a text and makes it stay in a window?

So, if you don't really know what do I mean, there's a small explanation.所以,如果你真的不明白我的意思,这里有一个小的解释。 I'm also new in C++ so I need help with some things, so yeah.我也是 C++ 的新手,所以我需要一些帮助,所以是的。 There's the explanation.这就是解释。

If you make a code, for example例如,如果您制作代码

cout << "Hello world!";```
-- I forgot how to make it fully good, but that's an example

Then it prints and closes extremelly fast.然后它打印和关闭非常快。 So, how can I make it so that it DOESN'T dissapear?那么,我怎样才能让它不会消失呢?

I am assuming you are talking about console window that closes fast.我假设您正在谈论快速关闭的控制台 window。 In order to prevent that there are multiple ways of doing it.为了防止有多种方法可以做到这一点。 But i will write two simplest ways of doing so.但我会写两种最简单的方法。

  1. Add header at the top of your C++ program在 C++ 程序的顶部添加 header

     #include <stdlib.h>

Then in the end of main function just before return 0 write this line of code然后在main function的最后return 0之前写下这行代码

system("pause");
  1. Another way of doing this is to get any character input before return 0 .另一种方法是在return 0之前获取任何字符输入。 So write these lines of code所以写这几行代码

     Console.WriteLine("Press any key to exit..."); Console.ReadKey();

One of the simplest solutions is to use std::getchar() or std::cin.get() .最简单的解决方案之一是使用std::getchar()std::cin.get() The program will wait for user input.程序将等待用户输入。 However, if there are already characters in the input buffer, then the program, roughly speaking, will ignore and will not wait for input from the user.但是,如果输入缓冲区中已经有字符,那么程序大致会忽略并且不会等待用户输入。

If the program is specific to Windows, then one of the most common solutions is to use std::system("pause") .如果程序特定于 Windows,那么最常见的解决方案之一是使用std::system("pause")

For std::system() , you must also include the cstdlib header.对于std::system() ,您还必须包含cstdlib header。

But it would still be better to set a breakpoint at the end of the program or, if your IDE allows (for example Visual Studio), then enable the option that leaves the console opened even after exiting.但最好在程序结束时设置断点,或者,如果您的 IDE 允许(例如 Visual Studio),则启用即使在退出后仍打开控制台的选项。 For example, VS allows you to run a program in non-debug mode, which does not close the console.例如,VS 允许您以非调试模式运行程序,该模式不会关闭控制台。 To run the program in this mode, you can press the key combination Ctrl + F5 .要在此模式下运行程序,您可以按组合键Ctrl + F5

However, in this case, in the project settings there should be Subsystem option in the System section in the Linker.但是,在这种情况下,在项目设置中,Linker 的 System 部分中应该有Subsystem选项。 And the value of Subsystem should be /SUBSYSTEM:CONSOLE .并且Subsystem的值应该是/SUBSYSTEM:CONSOLE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM