【发布时间】:2020-07-31 11:28:42
【问题描述】:
我正在尝试在 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 << " ";
}
}
}
【问题讨论】:
-
与其描述您的代码,不如以文字形式发布,minimal reproducible example。图片不会粘贴到 IDE 中,指向网站的链接会中断或被防火墙阻止。
-
完成。带有编译输出和 launch.json 的 hello cpp 应该是可重现的。
-
要在最后暂停代码,我通常使用
std::cout << "Paused. Press ENTER to continue"; std::cin.ignore(1000000, '\n');这会等到按下 ENTER 或输入给定数量的字符。 -
在这个链接有一个视频,我在 VS 代码上,在 MacOS 上的确切代码,工作得很好 youtu.be/-erXR6k9TeE?t=1280>
-
我尝试听从您的建议,但并没有太大变化。外部终端打开,但它只是我家中的一个提示,它没有运行我的程序。
标签: c++ macos visual-studio-code vscode-debugger