Visual Studio代码:从用户那里获取输入 [英] Visual Studio Code: Take Input From User

查看:288
本文介绍了Visual Studio代码:从用户那里获取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Currently, I'm trying to write C/C++ program in Visual Studio code. For this I've installed two extensions: C/C++ & C++ Intellisense

根据文档,调试功能不适用于Windows。我已经能够使用以下任务来构建和运行代码:

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:

{
    "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"
            ]
        }
    ]
}

和一个简单的 Makefile

all: clean helloworld

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

clean:
    C:/Programs/cygwin/bin/rm -rf helloworld

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

But, the problem arises, when the programs needs some user input while running. Suppose for this very familiar helloworld program.

# 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.

推荐答案

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

Go to File > Preferences -> User Settings and add custom settings:

{
   "code-runner.runInTerminal": true
}

最后运行您的c ++代码,您将能够在控制台中输入值

Finally run your c++ code and you will be able to enter values in console

这篇关于Visual Studio代码:从用户那里获取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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