Simple assignment of a variable to string function

76 Views Asked by At

I am brand new in programming. I keep getting the error what(): basic_string::at: __n (which is 14) >= this->size() (which is 14)

void longword(string word) {
    int length = word.length();
    if (length > 10)
    {
        cout << word[0] << length << word[length];
    }
    else
    {
        cout << word;
    }
}

int main() {
    string word;
    int n;
    cin >> n;
    for (int a = 0; a < n; a++)
    {
        cin >> word;
        longword(word);
        cout << endl;
    }
}
1

There are 1 best solutions below

4
schteppe On

word[length] is out of bounds. word[length-1] will give you the last character in the word.