VS代码调试控制台输入

时间:2020-04-17 20:53:17

标签: 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"
}
]
}

当我按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 << " ";
        }
    }
}

1 个答案:

答案 0 :(得分: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
        }
    ]
}

点击运行按钮后,VSCode将要求您提供pid: enter image description here

现在,将能够调试: enter image description here