简单的"Hello World"样式程序在执行开始后很快关闭 [英] Simple "Hello World" style program closes extremely soon after execution starts

查看:46
本文介绍了简单的"Hello World"样式程序在执行开始后很快关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过一本名为 C ++ A Beginners Guide Second Edition 的书来学习C ++.当我运行可执行文件时,它将显示半秒钟并关闭它.

I am learning C++ through a book called C++ A Beginners Guide Second Edition. When I run the executable, It displays it for half a second and closes it.

我在Windows 8.1上使用Microsoft Visual Studio Express 2013 for Windows桌面.

I am using Microsoft Visual Studio Express 2013 for Windows Desktop on Windows 8.1.

这是代码:

*/
#include <iostream>
using namespace std;

int main()
{
    cout << "C++ is power programming.";

    return 0;

}

由于控制台关闭如此之快,我只能在运行时才能看到文本.

I can only just see the text when I run, as the console closes so quickly.

为什么程序关闭得如此之快,我该如何阻止这种情况发生?

Why does the program close so fast, and how do I stop that from happening?

'Project1.exe' (Win32): Loaded 'C:\Users\Benjamin\Documents\Visual Studio 2013\Projects\Project1\Debug\Project1.exe'. Symbols loaded.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp120d.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr120d.dll'. Cannot find or open the PDB file.
The program '[6908] Project1.exe' has exited with code 0 (0x0).

推荐答案

逐行浏览程序:

int main()
{

这定义了程序的入口点,它返回的 int 将返回到启动该程序的任何位置.

This defines the entry point for your program, and the int that it returns will be returned to whatever started the program.

    std::cout << "C++ is power programming."; // or just cout when you're using namespace std

这将字符串文字 C ++是强力编程.打印到控制台.

This prints the string literal C++ is power programming. to the console.

    return 0;
}

通常将返回0的值返回给调用方以指示成功(程序成功执行).但是,您可以根据需要返回其他内容(例如,如果您的程序计算了调用者应使用的某些值).

Returning a value of 0 to the caller is often used to indicate success (the program executed successfully). You could however, return something else if you wanted to (for example, if your program computes some value that should be used by the caller).

因此,总而言之,您告诉程序将消息打印到控制台,然后返回,这正是它的作用.如果您想在程序完成后立即停止其关闭,则可以在 return 0 语句之前进行如下操作:

So, in a nutshell, you tell your program to print a message to the console and then return, which is exactly what it does. If you wanted to stop the program from closing as soon as it's finished, you could something just before the return 0 statement like this:

std::cin.get(); // or just cin.get() when using namespace std
return 0;

std :: cin.get()的作用是等待用户输入;准备好后,按Enter键即可结束程序.

What std::cin.get() does is wait for user input; pressing enter should end your program when you're ready.

这篇关于简单的"Hello World"样式程序在执行开始后很快关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆