'C++ Vscode Terminal Input Doesn't Work For Dynamic Input i.e. Vectors/For loops

I've just started coding in C++ with VSCode and I followed the setup here. It works perfectly with the coderunner extension in the terminal, however I am unable to use std::cin inside of a for loop. I can use it on a fixed number of times, like:

int x;
cin >> x;

but as soon as I use dynamic input, like

int x = 5;
vector<long long> a(x);
for (int i = 0; i < x; i++)
{
    cin >> a[i]; 
}

my program stops working. Originally the terminal would look like this, indicating the program is running and awaiting input

enter image description here

but while using a loop the program looks like thisenter image description here and input does not work.

My settings.json file looks like this:

{
"C_Cpp.errorSquiggles": "Enabled",
"code-runner.runInTerminal" : true,

"files.associations": {
    "iostream": "cpp",
    "vector": "cpp",
    "algorithm": "cpp",
    "chrono": "cpp",
    "complex": "cpp",
    "cstdlib": "cpp",
    "valarray": "cpp"
}

and my c_cpp_properties.json like this:

{
"configurations": [
    {
        "name": "Win64",
        "includePath": [
            "${workspaceFolder}/**",
            "${workspaceFolder}"
        ],
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE"
        ],
        "windowsSdkVersion": "10.0.19041.0",
        "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.31.31103/bin/Hostx64/x64/cl.exe",
        "cStandard": "c17",
        "cppStandard": "c++17",
        "intelliSenseMode": "windows-msvc-x64"
    }
],
"version": 4
}

The extensions I have installed are: Better C++ Syntax, C/C++, C/C++ Compile Run, C/C++ Extension Pack, C/C++ Themes, CMake, and CMake Tools

Any help would be greatly appreciated!!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source