問題描述
目前,我正在嘗試用 Visual Studio 代碼編寫 C/C++ 程序.為此,我安裝了兩個擴展:C/C++ &C++ 智能感知
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.
推薦答案
轉到 Code ->首選項 ->設置
并添加自定義設置:
Go to Code -> Preferences -> 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 Code:從用戶獲取輸入的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!