Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unexpected Run time error in VS code while working with strings

#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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

string input; is empty so input[i] accesses the string out of bounds which makes your program have undefined behavior. You could resize it to size to make it work – or create the string with the correct size after you’ve entered what size you want it to have.

Example:

#include <iostream>
#include <string>

int main() {
    std::string input;
    int size;
    std::cout << "Enter the size of your string value\n";
    if(std::cin >> size && size > 0) {

        input.resize(size); // resize the string
        // std::string input(size, '\0'); // or create it here with the correct size

        std::cout << "Enter the string whose first letter has to be changed\n";

        for(char& ch : input) { // a simpler range-based for loop
            std::cin >> ch;
        }
        input[0] = 'Z';
        std::cout << "The changed string is ";
        std::cout << input << '\n'; // no need to loop here
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading