asn
asn

Reputation: 2587

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

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?

Upvotes: 20

Views: 43528

Answers (9)

bogdanbm
bogdanbm

Reputation: 1

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)

Upvotes: 0

Saqib Ayoub
Saqib Ayoub

Reputation: 1

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.

Upvotes: 0

Kuznetsov-M
Kuznetsov-M

Reputation: 435

  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

Upvotes: 8

Ajay jangid
Ajay jangid

Reputation: 973

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.

Upvotes: 1

Union find
Union find

Reputation: 8150

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.

Upvotes: 0

Joseph Pena
Joseph Pena

Reputation: 370

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

Upvotes: 10

tachyon
tachyon

Reputation: 21

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.

Upvotes: 2

傅继晗
傅继晗

Reputation: 1137

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

Upvotes: 0

asn
asn

Reputation: 2587

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.

Upvotes: 38

Related Questions