Unexpected Run time error in VS code while working with strings

  c++, gcc, runtime-error, stdstring, visual-studio-code
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string input;
    int size;
    cout << "Enter the size of your string value" << endl;
    cin >> size;
    cout << "Enter the string whose first letter has to be changed" << endl;

    for (int i = 0; i < size; i++)
    {
        cin >> input[i];
    }

    input[0] = 'Z';
    cout<<"The changed string is ";

    for (int i = 0; i < size; i++)
    {
        cout << input[i];
    }

    return 0;
}

While running this in VS Code the following error is being shown after entering the string:

Enter the size of your string value
4
Enter a string whose first letter has to be changed
moya
/home/keith/builds/mingw/gcc-9.2.0-mingw32-cross-native/mingw32/libstdc++- 
v3/include/bits/basic_string.h:1067: std::__cxx11::basic_string<_CharT, _Traits, 
_Alloc>::reference std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator[] 
(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; 
_Traits = std::char_traits<char>; _Alloc = std::allocator<char>; 
std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference = char&; 
std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]: 
Assertion '__pos <= size()' failed.

This code is running properly in other online c++ compilers but is not running in VS Code. I have no idea what the problem is. Please find the problem here.

Source: Windows Questions C++

LEAVE A COMMENT