Vscode-cpptools: How to read user input (std iostream) when debugging a C++ program ?

Created on 14 May 2020  路  5Comments  路  Source: microsoft/vscode-cpptools

Type: Debugger

It might related/duplicate of https://github.com/Microsoft/vscode-cpptools/issues/449 but following @pieandcakes suggestion, I am opening a new issue.

Describe the bug
I can't read stdin while debugging C++ program

To Reproduce

I believe it is not a bug, it is much more like a question / missing step.

So I just follow the debugging tutorial. Nothing special in my code, I also put breakpoints in every line :
image

So what I expect is since I have stopAtEntry: true, when I run "start debugging", the debugger will halt the process at my breakpoint, highlight it with a yellow background. Then when it reach cin line, I can write my input so the program will continue to execute. What actually happen is it only pop up an external empty terminal but it doesn't seem to run my code, It is not prompting me any input. (Note: In my code I wrote an output to std::out in the first line)

cout << "How old are you ?" << endl;

Here is my screen recording
not_working

So what I miss ?

Additional Context
I am mac user, here is my OS specification :
image

Here is my launch.json & task.json file :
Launch.json (I turned the 'externalConsole' parameter to true, as suggested in https://github.com/Microsoft/vscode-cpptools/issues/449)

{
  // 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": "clang++ - Build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "stopAtEntry": true,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": true,
      "MIMode": "lldb",
      "preLaunchTask": "clang++ build active file",
    }
  ]
}

Task.json :

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "C/C++: clang++ build active file",
      "command": "/usr/bin/clang++",
      "args": [
        "-std=c++17",
        "-stdlib=libc++",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}",
      ],
      "options": {
        "cwd": "${workspaceFolder}"
      },
      "problemMatcher": [
        "$gcc"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    },
    {
      "type": "shell",
      "label": "clang++ build active file",
      "command": "/usr/bin/clang++",
      "args": [
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}"
      ],
      "options": {
        "cwd": "/usr/bin"
      }
    }
  ]
}

I only install c++ extension,
image

VsCode version :

Version: 1.45.0
Commit: d69a79b73808559a91206d73d7717ff5f798f23c
Date: 2020-05-07T15:57:33.467Z (6 days ago)
Electron: 7.2.4
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0
OS: Darwin x64 18.7.0

Here is my debug console output :

debugger question

Most helpful comment

Thanks after following your temporary workaround there , it works like a charm !

All 5 comments

The only way I am able to read user input is by enabling "externalConsole", I haven't found a way to use the integrated console in VSC. If "externalConsole" isn't working for you, then I am not sure how else you would be able to input something. Would be interested to see if anyone has a solution to this, especially for the ability to use the integrated console.

From the logs and the gif of the terminal just opening up and displaying the default shell prompt suggested that you may be running into this issue https://github.com/microsoft/vscode-cpptools/issues/5079.

We do not have support for reading from stdin with the integrated terminal on macOS.

From the logs and the gif of the terminal just opening up and displaying the default shell prompt suggested that you may be running into this issue https://github.com/microsoft/vscode-cpptools/issues/5079.

We do not have support for reading from stdin with the integrated terminal on macOS.

How would one be able to read from the integrated terminal on Windows? Thank you.

Thanks after following your temporary workaround there , it works like a charm !

@DrVrej Can you open up another issue with what you are trying on the Windows Integrated Terminal?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DemoCrazer picture DemoCrazer  路  3Comments

ecbrodie picture ecbrodie  路  3Comments

jyavenard picture jyavenard  路  3Comments

wdc596933938 picture wdc596933938  路  3Comments

CDitzel picture CDitzel  路  3Comments