首页 > 解决方案 > 整数或浮点变量的 C++ cin/coutSegmentation 错误,但对于 char 类型没问题

问题描述

cin >> num;整数类型在线存在分段错误。

#include <iostream>
using namespace std;
int main(void)
{
    int num;

    printf("Hello World\n");

    cin >> num; // seg fault line

    system("pause");
    return 0;
}

数字。整数错误

虽然 char 类型运行良好。

#include <iostream>
using namespace std;
int main(void)
{
    char str;

    printf("Hello World\n");

    cin >> str;

    system("pause");
    return 0;
}

数字。适合 char

我正在使用 VSCode 和 MinGW,c_cpp_properties.json 如下:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\MinGW\\bin\\gcc.exe",
            "cStandard": "gnu11",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "gcc-x86"
        }
    ],
    "version": 4
}

标签: c++visual-studio-code

解决方案


推荐阅读