1

I have the following C application:

#include <stdio.h>
#include <string.h>
int main() {
    char name[10];
    printf("What is your name?\n");
    fflush(stdout);
    fscanf(stdin, "%s", name);
    printf("Your name is %s.\n", name);
    return 0;
}

I am running it with:

  • Visual Studio Code 1.48.2
  • C/C++ (Extension for Visual Studio Code) 0.29.0
  • Code Runner (Extension for Visual Studio Code) 0.11.0
  • MinGW (mingw32-base) 2013072200

When I hit CTRL+F5 (effectively, Run Without Debugging), I see the prompt, What is your name? in the Debug Console. I try to type my name into the Debug Console, and it gives the following error: Unable to perform this action because the process is running.

How can I read from stdin using the integrated terminal?

Here is my launch.json configuration:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc.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++: gcc.exe build active file"
        }
    ]
}

I was able to get it to take input from stdin using the external console by setting "externalConsole": true, in the above configuration, but I am looking for a way, if possible, to be able to simply write my name in one of the integrated IDE console tabs so that I don't need to launch an external console.

Alexandru
  • 12,264
  • 17
  • 113
  • 208
  • 1
    OT: When you `printf` with a `\n` there is no need to `fflush(stdout);` – David Ranieri Sep 24 '20 at 19:40
  • 2
    @DavidRanieri That would be true if the line buffering is respected in the specific console. But unfortunately some IDE embedded consoles are buggy with this respect. – Eugene Sh. Sep 24 '20 at 19:47
  • @EugeneSh. I don't think that's the case for Visual Studio Code (I'm saying I don't think so because I don't use it, but it would be pretty sloppy) – David Ranieri Sep 24 '20 at 19:51
  • @DavidRanieri If you did use it, you would know that you will not see debug output right away while you are debugging the application with the same environment setup if you do not flush it like this. Can we please stick to the topic? – Alexandru Sep 24 '20 at 20:12
  • 1
    Does this answer your question? [Visual Studio Code: Take Input From User](https://stackoverflow.com/questions/36964949/visual-studio-code-take-input-from-user) – Eugene Sh. Sep 24 '20 at 20:19
  • 1
    @Alexandru 1) There was an OT mark before my comment. 2) There is nothing wrong with pointing out a part of the code in the comments, I have not put it as an answer, just tone it down a little bit. – David Ranieri Sep 24 '20 at 20:24
  • @EugeneSh. I tried setting the `Code-runner: Run in Terminal` option under `File -> Preferences -> Settings` but this did not work for me. – Alexandru Sep 24 '20 at 21:12
  • @EugeneSh. I will try to see if it works with `MinGW-w64` as mentioned in a comment on the thread you linked. Although, I looked online and found that this is an open issue with the integrated terminal, since it should also work with `MinGW`: https://github.com/microsoft/vscode-cpptools/issues/5497 – Alexandru Sep 24 '20 at 21:31

1 Answers1

1

I'm not an English speaker, so I apologize if my writing is strange.

I'm posting this because a similar case occurred to me and I was able to solve it.

I used this as a reference. (https://github.com/microsoft/vscode-cpptools/issues/5497#issuecomment-628878810)

In my case, the integrated terminal was not accepting keyboard input.

The solution was to change the compiler to this. (https://sourceforge.net/projects/mingw-w64/)

And you probably need to create a path and then rewrite tasks.json and launch.json

Here are my tasks.json and launch.json as an example. Please refer to it.

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe アクティブなファイルのビルド",
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "デバッガーによって生成されたタスク。"
        }
    ],
    "version": "2.0.0"
}
{
    // IntelliSense を使用して利用可能な属性を学べます。
    // 既存の属性の説明をホバーして表示します。
    // 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - アクティブ ファイルのビルドとデバッグ",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "gdb の再フォーマットを有効にする",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe アクティブなファイルのビルド"
        }
    ]
}
sin471
  • 11
  • 1