Home >Backend Development >C++ >How Can I Provide Standard Input to My C/C Program When Debugging in Visual Studio Code?

How Can I Provide Standard Input to My C/C Program When Debugging in Visual Studio Code?

Linda Hamilton
Linda HamiltonOriginal
2024-12-01 12:56:13277browse

How Can I Provide Standard Input to My C/C   Program When Debugging in Visual Studio Code?

Writing to Standard Input in Visual Studio Code with C/C Extension

Debugging a C/C program often requires the ability to provide user input during execution. This can be challenging when using the C/C extension in Visual Studio Code, especially for Windows users since the debugging facility is unavailable.

Current Implementation:

You have successfully configured Visual Studio Code to build and run your code using a Makefile and custom tasks. However, to receive user input at runtime, you need to enable console input.

Solution:

  1. Open Visual Studio Code's settings (Code -> Preferences -> Settings).
  2. Scroll down to the "User Settings" section.
  3. Add the following custom setting:
{
   "code-runner.runInTerminal": true
}
  1. Save the settings.

Explanation:

The code-runner.runInTerminal setting launches the program in the integrated terminal. This allows for standard input while the program is running.

Example:

Consider the following helloworld.cpp program:

#include <iostream>

using namespace std;

int main() {
  string name;

  cout << "Enter your name: ";
  cin >> name;

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

  return 0;
}

When you run this program with the modified settings, the terminal will prompt you for input and pass it to the program as standard input.

The above is the detailed content of How Can I Provide Standard Input to My C/C Program When Debugging in Visual Studio Code?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn