在Visual Studio Code中以C ++进行调试时,如何读取输入? [英] How to read input when debugging in C++ in Visual Studio Code?

查看:103
本文介绍了在Visual Studio Code中以C ++进行调试时,如何读取输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 VSCode 调试 MacOSX 中的 CPP 程序.

I'm using VSCode for debugging my CPP program in MacOSX.

我有2个程序.

程序1

int main(){

    string a;
    a = "a";
    a += 'b';
    cout<<a<<endl;
    return 0;
}

程序2

int main(){

    string a;
    cin>>a;
    a += 'b'
    cout<<a;
    return 0;
}

program1 中,我直接为 string a 分配代码,并在 VSCode 中调试程序时,首先使用以下命令在终端中对其进行编译:

In program1 I'm directly assigning the string a and when I debug the program in VSCode by first compiling it in terminal using :

g ++ -g filename.cpp

,然后在 Debugging菜单中选择 Starting Debugging 选项.我可以通过在断点处向前移动来查看 string a 变量的状态.

and then selecting the Starting Debugging option in the Debugging menu. I'm able to see the state of the string a variable by moving forward in breakpoints.

变量部分显示了不同变量的状态,而 CALL STACK 显示了堆栈框架.

The VARIABLES section shows the state of different variables and the CALL STACK show the stack frame.

但是,对于 program2 ,当我经过 cin>> a; 的断点时,变量呼叫堆栈被清除.

But, for program2, when I go past the breakpoint of the cin>>a;, the contents of VARIABLES and of CALL STACK get cleared up.

以下是 launch.json 文件的内容:

{
    "version": "0.2.0",
    "configurations": [    
        {
            "name": "(lldb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb"
        }
    ]
}

如何获取用户输入并继续调试我的代码?

How can I get user-input and move forward to debug my code?

推荐答案

中所述

如果在 launch.json 中启用"externalConsole":true ,那么您将获得一个可以键入的弹出控制台窗口.

if you enable "externalConsole":true in the launch.json then you will get a pop up console window that you can type in.

这篇关于在Visual Studio Code中以C ++进行调试时,如何读取输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆