Visual Studio命令提示符关闭,尽管我试图停止它与cin.get() [英] Visual Studio command prompt closes despite that I tried to stop it with cin.get()

查看:174
本文介绍了Visual Studio命令提示符关闭,尽管我试图停止它与cin.get()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的下面的基本C ++代码不工作。当我在Visual Studio express中运行它时,它只是在我输入第二个数字后关闭命令提示符。先感谢。

  #include< iostream> 

using namespace std;

int main()
{

int num1,num2,answer;

cout<< 输入数字:;
cin>> num1;
answer = num1 * num1;
cout<< 数字的总和是:< answer<< endl;

cout<< 输入第二个数字;
cin>> num2;
answer = answer + num2;
cout<< 这两个数字的总和是:< answer<< endl;

cin.get();
return 0;
}


解决方案

问题是字符留在输入缓冲区。



无论如何,你不应该添加棘手命令来保持控制台打开(你必须记住从生产

CTRL + F5 ),Visual Studio将保持控制台应用程序窗口打开,直到您按下按钮(只需检查 Project - >属性 - >链接器 - >系统 - >子系统 - >控制台(/ SUBSYSTEM:CONSOLE))。



当然,如果你正在调试( F5 返回0上的断点; 是最好的选项。


My following basic C++ code isn't working. When I run it in Visual Studio express, it just closes the command prompt after I enter the second number. Thanks in advance.

#include <iostream>

using namespace std;

int main()
{

    int num1, num2, answer;

    cout << " Enter a number: ";
    cin >> num1;
    answer = num1 * num1;
    cout << " the sum of the number is: " << answer << endl;

    cout << "Enter a 2nd number";
    cin >> num2;
    answer = answer + num2;
    cout << "The sum of the two numbers is: " << answer << endl;

    cin.get();
    return 0;
}

解决方案

The problem is that there are characters left in the input buffer.

Anyway you shouldn't add "tricky" commands to keep the console open (you have to remember to remove them from the "production" code).

You can run your program Without debugger mode (CTRL + F5) and Visual Studio will keep the console application window open until you press a button (just check the settings in Project -> Properties -> Linker -> System -> Sub System -> Console (/SUBSYSTEM:CONSOLE)).

Of course, if you are debugging (F5), a breakpoint on the return 0; is the best option.

这篇关于Visual Studio命令提示符关闭,尽管我试图停止它与cin.get()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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