18

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?

asn
  • 2,408
  • 5
  • 23
  • 37

9 Answers9

34

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.

asn
  • 2,408
  • 5
  • 23
  • 37
  • 1
    This does not seem to work with piping input from stdin like `./program < input.txt`. Is there an alternative for that? – Orestis Kapar Feb 23 '20 at 11:28
  • 3
    Turns out that it works if you put "<" and "input.txt" as separate arguments. – Orestis Kapar Apr 15 '20 at 23:18
  • sadly this isn't an option for me. the debugger never launches. same as https://stackoverflow.com/questions/53108690/visual-studio-code-c-debugger-doesnt-start – jozxyqk Mar 18 '21 at 19:19
  • 2
    This is outdated information. I just installed Mac CodeLLDB and it is now a little easier, but there are still a few steps. Watch a video walk-through this worked for me: https://www.youtube.com/watch?v=wKjFVyDbSpA – Scott Prive Jun 17 '22 at 15:28
9

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

Joseph Pena
  • 167
  • 2
  • 8
3
  1. Install extension CodeLLDB

  2. Add new configuration CodeLLDB: Launch

    Add new configuration

  3. Set program property as "program": "${workspaceFolder}/${fileBasenameNoExtension}"

  4. (optional) Rebuild code

  5. Chose created Launch config in VS Debug tab. And start it!

    Chose created Launch config in VS Debug tab

Profit! Demo Video manual

Kuznetsov-M
  • 346
  • 7
  • 18
2

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.

ϹοδεMεδιϲ
  • 2,790
  • 3
  • 33
  • 54
tachyon
  • 21
  • 2
1

simply:-
step1. click on small gear-icon of debugger window.
step2. make "true" to this ["externalConsole": false,] in launch.json file.
step3. and just restart your debugger.

Ajay jangid
  • 711
  • 9
  • 9
0

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

傅继晗
  • 927
  • 1
  • 8
  • 14
0

In my case this was a two-step process.

  1. Enable externalConsole: true, as described in other responses.
  2. Let VS code control the terminal.
Union find
  • 7,759
  • 13
  • 60
  • 111
0

If the code you are debugging requires user input, set external Console to true. after entering input, avoid clicking "x" to close the external Console. Instead, click "-" to minimise the window. Then keep hitting f10 or f11 to continue debugging.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • I tried this, What my VS code does is, it opens entirely new terminal instance ( rather using VS-code terminal , which opens in bottom part ) . I was able to do debugging, but lot of switching between windows. Any idea how to solve it ? – Neer Patel Jan 25 '23 at 09:04
  • There is no need to switch windows. Simply divide the computer screen in half, keeping the VS code and the debugging window on either side. And keep debugging – Saqib Ayoub Jan 26 '23 at 10:51
0

externalConsole: true, allows me to type input in the external console but the redirection method "args":["<","in.txt"], does NOT

Using: VSCode 1.80.2 with C/C++ Extension from Microsoft v1.16.3 and Clang Apple clang version 14.0.3 (clang-1403.0.22.14.1) Target: x86_64-apple-darwin22.6.0 Thread model: posix

macOS: Ventura 13.5 (22G74)

bogdanbm
  • 1
  • 1