C++ Visual Studio Code:从用户获取输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36964949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Studio Code: Take Input From User
提问by Arnab Das
Currently, I'm trying to write C/C++ program in Visual Studio code. For this I've installed two extensions: C/C++& C++ Intellisense
目前,我正在尝试用 Visual Studio 代码编写 C/C++ 程序。为此,我安装了两个扩展:C/C++和C++ Intellisense
As per the documentation, the debugging facility is not available for windows. I've been able to build and run the code with the following tasks:
根据文档,调试工具不适用于 Windows。我已经能够通过以下任务构建和运行代码:
{
"version": "0.1.0",
"command": "cmd",
"isShellCommand": true,
"args": [
"/C"
],
"tasks": [
{
"taskName": "Makefile",
"suppressTaskName": true,
// Make this the default build command.
"isBuildCommand": true,
// Show the output window only if unrecognized errors occur.
"showOutput": "always",
// No args
"args": [
"C:/Programs/cygwin/bin/make.exe",
"all"
],
// Use the standard less compilation problem matcher.
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\d+):(\d+):\s+(warning|error):\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
},
{
"taskName": "Run",
"suppressTaskName": true,
"isTestCommand": true,
"args": [
"helloworld"
]
}
]
}
and one simple Makefile
:
一个简单的Makefile
:
all: clean helloworld
helloworld: helloworld.cpp
C:/Programs/cygwin/bin/g++ helloworld.cpp -o helloworld
clean:
C:/Programs/cygwin/bin/rm -rf helloworld
But, the problem arises, when the programs needs some user input while running. Suppose for this very familiar helloworld
program.
但是,当程序在运行时需要一些用户输入时,问题就出现了。假设对于这个非常熟悉的helloworld
程序。
# include <iostream>
using namespace std;
int main ()
{
int name;
cin >> name;
cout << "Hello, " << name << "!!!" << endl;
return 0;
}
Can you please help me to get the user input at run time. There is a work-around to pass the input as command line arguments. But, that is not possible for programs with complex flows.
你能帮我在运行时获取用户输入吗?有一种变通方法可以将输入作为命令行参数传递。但是,对于具有复杂流程的程序,这是不可能的。
回答by kaxi1993
Go to File > Preferences -> User Settings and add custom settings:
转到文件 > 首选项 - > 用户设置并添加自定义设置:
{
"code-runner.runInTerminal": true
}
Finally run your c++ code and you will be able to enter values in console
最后运行你的 C++ 代码,你将能够在控制台中输入值
回答by u10096812
if you encounter the same problem as me that the integratedTerminal cannot read input from user as below hanging(env. windows 10)
如果您遇到与我相同的问题,集成终端无法读取用户的输入,如下所示(环境。windows 10)
my solution was to replace cygwin's gdb and g ++ with mingw64's.
我的解决方案是用mingw64替换 cygwin 的 gdb 和 g ++ 。
回答by c0deManiac
As an added convenience, you can also supply inputs from a comment into the integrated terminal.
作为额外的便利,您还可以将注释中的输入提供给集成终端。
Example:
例子:
# include <iostream>
using namespace std;
int main ()
{
int name;
cin >> name;
cout << "Hello, " << name << "!!!" << endl;
return 0;
}
/*input
123
*/
Extension Link --
Comment Input: https://marketplace.visualstudio.com/items?itemName=virresh.cinp
扩展链接——
评论输入:https: //marketplace.visualstudio.com/items?itemName=virresh.cinp
Note: This extension depends on code-runner, another extension that can run scripts for almost all languages (and you can specify custom build options as well)
注意:这个扩展依赖于code-runner,另一个可以运行几乎所有语言脚本的扩展(你也可以指定自定义构建选项)
Disclaimer: I'm the author of this extension
免责声明:我是这个扩展的作者