簡體   English   中英

VS Code C 程序調試控制台不工作

[英]VS Code C Program Debug Console Not Working

我正在使用 VS Code IDE 在 macOS 上學習 C 語言。

下面是我試圖運行的 C 代碼。

#include <stdio.h>

int main(void)
{
    int number_from_user;

    /* Get number from user */
    printf("Please enter month number: ");
    scanf("%d", &number_from_user);

    /* Print month name */
    switch (number_from_user)
    {
        case 1:
            printf("January");
            break;
        case 2:
            printf("February");
            break;
        case 3:
            printf("March");
            break;
        case 4:
            printf("April");
            break;
        case 5:
            printf("May");
            break;
        case 6:
            printf("June");
            break;
        case 7:
            printf("July");
            break;
        case 8:
            printf("August");
            break;
        case 9:
            printf("September");
            break;
        case 10:
            printf("October");
            break;
        case 11:
            printf("November");
            break;
        case 12:
            printf("December");
            break;
        default:
            printf("Not a month");
            printf("Please run the program again");
            break;
    }
}

下面是launch.json文件。

{
    // 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": "gcc - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "lldb",
            "preLaunchTask": "C/C++: gcc build active file"
        }
    ]
}

我面臨的問題是,要使scanf function 工作,我需要將externalConsole設置為 true。 不幸的是,當我運行代碼時,它確實打開了終端,但它打開了一個空白終端,實際上並沒有運行代碼。

我做錯了什么,我該如何解決這個問題?

我安裝了插件 code-Runner,通常如果你已經安裝了 gcc 並且在你安裝它的那一刻就可以工作。

https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner

您只需單擊播放(或運行)按鈕,它會在 VS-Code 內自動打開一個控制台並運行實際程序。

如果問題只是scanf仍然存在,您可以嘗試將此 Go 到 File > Preferences -> User Settings 並添加自定義設置:

{
    "code-runner.runInTerminal": true
}

這是解釋的來源: https://github.com/formulahendry/vscode-code-runner/issues/72

ps:小心,只接受單文件程序,一開始是有用的,但以后可能會改成其他更復雜的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM