2

In Linux, I would be able to type into the integrated terminal no problem. I'd be able to type in user input and it would output. On Windows, I cannot do that. The output shows in the Debug Console and I cannot type into that or the integrated terminal.

In the picture, I run without debugging in C++ and when I ask for an input, it hangs there and doesn't output. I've seen CodeRunner but I rather not use that.

The picture of the terminal when running.

EDIT

{
    // 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": "g++.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file"
        }
    ]
}
  • Are you running this C++ program using the "Code Runner" extension? This extension will display the output in Debug Console, which disallow taking user input, by default – AnsonH Oct 17 '20 at 07:40
  • I don't run the programing using Code Runner. I've just been pressing Ctrl+F5 to run my code. I installed MinGW – Kevin C. Cucumber Oct 17 '20 at 12:49
  • I see. There should be a folder called ".vscode" inside the same directory where your C++ files are placed. Can you copy the contents from "launch.json" inside that ".vscode" folder, then edit this question by pressing the "edit" button below the question tags, paste the code you copied in the question and save the changes? This way I can see what's inside your launch,json file. Thanks! – AnsonH Oct 17 '20 at 15:22

1 Answers1

0

By default, the Debug Console which the C++ program is outputting to does not support user input. This means that typing your input in the Debug Console will not be read by the C++ program.

To solve this problem, change the line "externalConsole": false to "externalConsole": true in your launch.json file so that your C++ program can run in an external console. This way, you can enter your user input and get interpreted by the C++ program that is being debugged.

Your launch.json should now look something like:

{
    // 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": "g++.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,    // <-- Changed to "true" in here
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file"
        }
    ]
}

Read more here: How to read input when debugging in C++ in Visual Studio Code?

AnsonH
  • 2,460
  • 2
  • 15
  • 29
  • On vs code, the integrated terminal and the debug console are in two separate tabs. I've tried what you said before but I was approached by a different problem. But what I'm trying to do is type into the integrated terminal, not the debug console. The picture I've linked is a picture of the terminal. – Kevin C. Cucumber Oct 19 '20 at 14:20
  • VS Code currently does not support running the debugged C++ program inside the integrated terminal. The debugged program can only run in either the debug console or in the external console window. Only the program run in external console can read user input properly. Therefore, there's no way that you can type your input inside the integrated terminal. – AnsonH Oct 19 '20 at 15:18
  • 1
    dang. guess i'll just got with the external terminal. thank you! – Kevin C. Cucumber Oct 19 '20 at 16:08