How to read input when debugging in C++ in Visual Studio Code?

为君一笑 提交于 2020-12-01 12:00:30

问题


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

I've 2 programs.

Program1

int main(){

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

Program2

int main(){

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

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

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.

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

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

Here are the contents of the launch.json file:

{
    "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?


回答1:


As stated in Here

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




回答2:


To debug with inputs, you can edit the arg section as shown below:

"program": "${workspaceFolder}/main",
"args": ["<", "input_file.in"]

The example above should be the same as: ./main < input_file.in




回答3:


I hope this helps anyone who comes around here:

by (1) setting "externalConsole" to true and (2) checking (enabling) "Run In Terminal" in Code-Runner Extension configuration, you can plug-in your input to your code by typing the input on the external console, that would pop up when you run your code.




回答4:


I also encountered the same problem, my solution was to replace cygwin's gdb and g ++ with mingw64's.



来源:https://stackoverflow.com/questions/57494832/how-to-read-input-when-debugging-in-c-in-visual-studio-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!