Visual Studio代码:从用户输入

Arn*_*Das 9 c++ visual-studio-code

目前,我正在尝试用Visual Studio代码编写C/C++程序.为此,我安装了两个扩展:C/C++C++ Intellisense

根据文档,调试工具不适用于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"
            ]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

一个简单Makefile:

all: clean helloworld

helloworld: helloworld.cpp
    C:/Programs/cygwin/bin/g++ helloworld.cpp -o helloworld

clean:
    C:/Programs/cygwin/bin/rm -rf helloworld
Run Code Online (Sandbox Code Playgroud)

但是,当程序在运行时需要一些用户输入时,问题就出现了.假设这个非常熟悉的helloworld程序.

# include <iostream>

using namespace std;

int main ()
{
  int name;

  cin >> name;

  cout << "Hello, " << name << "!!!" << endl;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

能帮我在运行时获取用户输入吗?有一种解决方法可以将输入作为命令行参数传递.但是,对于流量复杂的程序来说,这是不可能的.

小智 19

确保您的 VS code 上安装了代码运行程序。

从顶部选择:文件 > 首选项 > 设置

在设置中搜索代码运行程序并选中该框:

代码运行器设置

默认情况下它保持未选中状态。您必须启用它才能在终端中运行您喜欢的语言。


kax*_*993 10

转到文件>首选项 - >用户设置并添加自定义设置:

{
   "code-runner.runInTerminal": true
}
Run Code Online (Sandbox Code Playgroud)

最后运行c ++代码,您就可以在控制台中输入值


Tan*_*mah 6

转到设置 (ctrl+,) -> 搜索设置 -> : Code-runner : 在终端中运行- 选中此项,您将能够直接在接受输入的终端中运行代码。:)