C++ cin/coutSegmentation Fault for integer or float vaiable but is ok for char type

Viewed 55

There is a Segmentation Fault on line cin >> num; for integer type.

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

    printf("Hello World\n");

    cin >> num; // seg fault line

    system("pause");
    return 0;
}

Figure. Error for integer

While the char type runs fine.

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

    printf("Hello World\n");

    cin >> str;

    system("pause");
    return 0;
}

Figure. Fine for char

I'm using VSCode and MinGW, the c_cpp_properties.json is as follow:

{
    "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
}
0 Answers
Related