【问题标题】:VScode Debugger output not shownVScode 调试器输出未显示
【发布时间】:2021-12-29 09:55:28
【问题描述】:

所以我刚开始学习编码,但 VS 代码表现得很奇怪?

当我在调试模式下运行我的代码时,代码有时不起作用。 当我要求时,代码的反应不同 “用户输入”VS“不要求用户输入”。

代码 - “不要求用户输入”

#include <stdio.h>
#include <stdlib.h>

int main(){
    enum Company {
        Google,
        FaceBook,
        XEROX,
        YAHOO,
        EBAY,
        Microsoft
    }COMPANY;
    // int test;
    // scanf("%i", test);
    // printf("%i",test);

    COMPANY = Google;
    printf("%d\n", COMPANY);

    int i;
    for (i= Google; i <= Microsoft; i++)
    {
        printf("%i\n", i);
    };
    return 0;
}

输出 Image of the Debug Console

CODE - “用户输入”

#include <stdio.h>
#include <stdlib.h>

int main(){
    enum Company {
        Google,
        FaceBook,
        XEROX,
        YAHOO,
        EBAY,
        Microsoft
    }COMPANY;
    int test;
    scanf("%i", test);
    printf("%i",test);

    COMPANY = Google;
    printf("%d\n", COMPANY);

    int i;
    for (i= Google; i <= Microsoft; i++)
    {
        printf("%i\n", i);
    };
    return 0;
}

输出:Output for debug console

这是我的启动 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.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "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"
        }
    ]
}

【问题讨论】:

  • scanf("%i", test); ==> scanf("%i", &amp;test); 启用编译器警告!另外不要使用%i,除非您希望允许用户以其他数字为基础输入数据。如果他们输入012,那是十进制的八进制,而不是十二。最好使用%d
  • 作为这里的新用户,也请收下tour并阅读How to Ask。此外,决定使用一种(!)编程语言,也请参阅您应用的标签的描述。此外,当您遇到代码问题时,请先提取minimal reproducible example
  • 另外,请不要对代码进行“进度”更新。

标签: c visual-studio-code vscode-debugger


【解决方案1】:
scanf("%i", test);

应该是

scanf("%i", &test)

scanf() 期望一个指向 int 的指针,但您传递的是一个 int

【讨论】:

  • 我改了,但问题还是一样
【解决方案2】:

除了调用scanf()中的语法错误

当您输入某个值时,您还必须输入一个“回车”键,以便将数字通过终端传递给程序。

【讨论】:

  • 你的意思是按 Enter 键?我做到了。但是你看甚至是一个要求它打印出它不打印出来的句子。即使 print 语句在 scanf 函数之前
猜你喜欢
  • 1970-01-01
  • 2014-04-20
  • 1970-01-01
  • 2021-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-13
  • 1970-01-01
相关资源
最近更新 更多