gpt4 book ai didi

c++ - Visual Studio Code : Take Input From User

转载 作者:可可西里 更新时间:2023-11-01 16:39:19 24 4
gpt4 key购买 nike

目前,我正在尝试在 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"
]
}
]
}

和一个简单的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 程序。

# include <iostream>

using namespace std;

int main ()
{
int name;

cin >> name;

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

return 0;
}

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

最佳答案

转到 Code -> Preferences -> Settings 并添加自定义设置:

{
"code-runner.runInTerminal": true
}

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

关于c++ - Visual Studio Code : Take Input From User,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36964949/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com