Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code: Take Input From User

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.

like image 586
Arnab Das Avatar asked May 01 '16 09:05

Arnab Das


People also ask

How do you take user input in VS Code?

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. :) Show activity on this post. Make sure you have code runner installed on your VS code.

How do you receive an input from a user?

The simplest way to obtain user input is by using the input() function. This function prompts the user for an input from the keyboard. Once the user has give the input and pressed Enter, the program flow continues. The input() function will always output a string.

How do I get input output in VS Code?

cpp inside the folder and give inputs in input. txt and press Ctrl+Shift+b and your output will be in the output.


Video Answer


2 Answers

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

like image 141
kaxi1993 Avatar answered Oct 19 '22 03:10

kaxi1993


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. :)

like image 18
Tanish Sarmah Avatar answered Oct 19 '22 03:10

Tanish Sarmah