39

Currently, I'm trying to write C/C++ program in Visual Studio code. For this I've installed two extensions: C/C++ & C++ Intellisense

As per the documentation, the debugging facility is not available for windows. I've been able to build and run the code with the following tasks:

{
    "version": "0.1.0",
    "command": "cmd",
    "isShellCommand": true,
    "args": [
        "/C"
    ],
    "tasks": [
        {
            "taskName": "Makefile",
            "suppressTaskName": true,
            // Make this the default build command.
            "isBuildCommand": true,
            // Show the output window only if unrecognized errors occur.
            "showOutput": "always",
            // No args
            "args": [
                "C:/Programs/cygwin/bin/make.exe",
                "all"
            ],
            // Use the standard less compilation problem matcher.
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}"
                ],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        },
        {
            "taskName": "Run",
            "suppressTaskName": true,
            "isTestCommand": true,
            "args": [
                "helloworld"
            ]
        }
    ]
}

and one simple Makefile:

all: clean helloworld

helloworld: helloworld.cpp
    C:/Programs/cygwin/bin/g++ helloworld.cpp -o helloworld

clean:
    C:/Programs/cygwin/bin/rm -rf helloworld

But, the problem arises, when the programs needs some user input while running. Suppose for this very familiar helloworld program.

# include <iostream>

using namespace std;

int main ()
{
  int name;

  cin >> name;

  cout << "Hello, " << name << "!!!" << endl;

  return 0;
}

Can you please help me to get the user input at run time. There is a work-around to pass the input as command line arguments. But, that is not possible for programs with complex flows.

Arnab Das
  • 3,548
  • 8
  • 31
  • 43
  • did you find the answer to that question? – vmg May 01 '17 at 18:19
  • @vmg no I couldn't find the answer anywhere. Recently I have not checked. But at that, I came into a conclusion, that this has to be supported from the plugin level, that's what running the code. – Arnab Das May 09 '17 at 02:42
  • Actually I was able to find the answer: https://github.com/thecoderok/cpp-vscode/blob/master/.vscode/launch.json . Use `"terminal": "integrated"` – vmg May 09 '17 at 16:56
  • Looks to be an open issue at the moment: https://github.com/microsoft/vscode-cpptools/issues/5497 – Alexandru Sep 24 '20 at 21:32

6 Answers6

49

Go to Code -> Preferences -> Settings and add custom settings:

{
   "code-runner.runInTerminal": true
}

Finally run your c++ code and you will be able to enter values in console

kaxi1993
  • 4,535
  • 4
  • 29
  • 47
  • 1
    this worked for me, thanks... I also was running it from the actual terminal by navigating to the file path, but this is much easier @kaxi1993 – pythlang Jun 22 '18 at 16:09
  • This didn't work for me. I tried to add the suggested line in settings.json. The terminal output goes to the Debug console still. – paolov Jun 03 '20 at 04:00
  • @paolov just check that you have added this setting at the root level. Means on a level where code-runner.executorMapByFileExtension exists. – kunj choksi Apr 14 '21 at 03:24
  • For whomsoever who cannot find it in latest versions. Go to help> all commands > type preferences> select preferences: language-specific settings > select c++ > paste the above code after 3rd line, it will work. – Pavan Kumar V Mar 16 '22 at 11:07
  • for the record, the question being asked here does not state that they are using that extension, while it states it is using others. – starball May 08 '23 at 23:39
19

Go to settings (ctrl+,) -> Search settings -> : Code-runner : Run in terminal - Check this and you will be able to run the code directly in the terminal which takes input. :)

Tanish Sarmah
  • 430
  • 5
  • 14
19

Make sure you have code runner installed on your VS code.

From top select: File > Preferences > Settings

Search for code runner in settings and check the box:

code runner settings

By default it remains unchecked. You have to enable it to run your preferred languages in terminal.

Mubtasim Fuad
  • 339
  • 2
  • 4
4

go to "settings", and search for "code-runner: Run in terminal" and check it. That's how i fixed it.

2

As an added convenience, you can also supply inputs from a comment into the integrated terminal.

Example:

# include <iostream>
using namespace std;
int main ()
{
  int name;
  cin >> name;
  cout << "Hello, " << name << "!!!" << endl;
  return 0;
}
/*input
123
*/

Extension Link --
Comment Input: https://marketplace.visualstudio.com/items?itemName=virresh.cinp

Note: This extension depends on code-runner, another extension that can run scripts for almost all languages (and you can specify custom build options as well)

Disclaimer: I'm the author of this extension

c0deManiac
  • 97
  • 1
  • 4
0

if you encounter the same problem as me that the integratedTerminal cannot read input from the user as below hanging(env. windows 10) enter image description here

my solution was to replace cygwin's gdb and g ++ with mingw64's.

then the input/output are normal enter image description here

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