VS Code 调试控制台输入

Kar*_*arl 4 c++ macos visual-studio-code vscode-debugger

我正在尝试在 MacOS 上使用 VS 代码调试以下 try C++ 程序。它需要用户的输入。它只是将两个数字作为输入并返回一个数字列表作为输出的东西。这是我的launch.json

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/hello",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb"
}
]
}
Run Code Online (Sandbox Code Playgroud)

当我按 F5 时,确实启动了一个外部终端窗口,但它不执行输出文件“hello”,它只是在我的主文件夹中显示常规提示〜。如果我只是正常运行文件,一切都会正常运行。我将发布我正在尝试调试的确切代码作为示例,因为它足够简单。


#include <iostream>
using namespace std;

class Calculator
{
private:
    /* data */
public:
    Calculator(/* args */) {}
    ~Calculator() {}
    void PrimeGenerator(int, int);
};


int main(int argc, char *argv[])
{
    cout << "Please enter two numbers: " << endl;
    int x, y;
    cin >> x >> y;
    Calculator c;
    c.PrimeGenerator(x,y);

    cin.ignore();
    cin.get();

    return 0;
}

void Calculator::PrimeGenerator(int x, int y)
{
    for (int i = x; i < y; i++)
    {
        bool prime = true;
        for (int j = 2; j * j <= i; j++)
        {
            if (i % j != 0)
            {
                prime = false;
                break;
            }
        }

        if (prime==true) {
            cout << i << " ";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Ara*_*kis 5

代码本身似乎没有问题。

您对外部控制台有严格的要求吗?如果没有,可以在终端中手动运行应用程序,然后附加:

{
    "name": "(lldb) Attach",
    "type": "cppdbg",
    "request": "attach",
    "program": "${workspaceFolder}/hello",
    "processId": "${command:pickProcess}",
    "MIMode": "lldb",
    "setupCommands": [
        {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

点击运行按钮后,VSCode 会询问你的 pid: 在此处输入图片说明

现在,将能够调试: 在此处输入图片说明