Abi T.
Abi T.

Reputation: 65

In C++, I'm getting a compiler error that I can't make sense of

Here's my code, it just reverses the sentence:

#include <iostream>
#include <string>   

using namespace std;

int main()
{
    string sentence;
    string reversedSentence;
    int i2 = 0;

    cout << "Type in a sentence..." << endl;
    getline(cin, sentence);

    for (int i = sentence.length() - 1; i < sentence.length(); i--)
    {
        reversedSentence[i2] = sentence[i];
        i2++;
    }

    cout << reversedSentence << endl;
}

Compilation works fine, but when I try to run the program, this happens:

Type in a sentence...
[input]
/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.

Upvotes: 0

Views: 103

Answers (5)

Yunfei Chen
Yunfei Chen

Reputation: 626

for (int i = sentence.length() - 1; i < sentence.length(); i--)

Why are you writing an infinite for loop?? you are decreasing the i, it will always be less then sentence.length().... I would use:

for(int i = sentence.length() - 1; i > 0; i--)

if I were you....

Upvotes: 0

sweenish
sweenish

Reputation: 5192

#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>

int main()
{
    std::string sentence;

    std::cout << "Sentence: ";
    std::getline(std::cin, sentence);

    std::stringstream sstrin(sentence);
    int spaces = std::count(sentence.begin(), sentence.end(), ' ');
    std::vector<std::string> chunks;
    chunks.reserve(spaces + 1);

    while (sstrin) {
        std::string tmp;
        sstrin >> tmp;
        chunks.push_back(tmp);
    }

    std::ostream_iterator<std::string> strout(std::cout, " ");
    std::cout << "Words in reverse:\n";
    std::copy(chunks.rbegin(), chunks.rend(), strout);

    std::cout << "\nFull Mirror:\n";
    std::ostream_iterator<char> charout(std::cout);
    std::copy(sentence.rbegin(), sentence.rend(), charout);
    std::cout << '\n';
}

It's a bit unclear what you mean by reverse. Your code indicates what I'd call a "full mirror" where the order of all the characters is backwards. But there's the possibility that you just want the words backwards, which is the first thing I think of when people say they want a sentence in reverse. Depending on which you want, the way you go about it differs.

For words in reverse, I need to ensure each word is treated as its own entity. For this I declare a vector of strings where each element will be an individual word. I use a stringstream since it will separate the words for me, without having to write a function to manually examine the sentence. This is because streams separate on whitespace by default. Once the vector is filled up with the words, I go about printing them in reverse order. I do this with a std::ostream_iterator. It's just more compact than a range-based for, and allows me to take advantage of the vector's reverse iterators; it saves me trouble of reversing the vector.

For the "full mirror," I similarly use a std::ostream_iterator, but it's of characters, because I feed the original sentence's reverse iterators. The individual elements of the string are characters.

Finally, there is no indication on whether you need to save these reversed sentences, so I don't bother.

Upvotes: 0

R Sahu
R Sahu

Reputation: 206567

My suggestion: Don't use indices. Prefer to use iterators whenever you can.

for (auto iter = sentence.rbegin(); iter != sentence.rend(); ++iter)
{
    reversedSentence.push_back(*iter);
}

Upvotes: 0

user12592944
user12592944

Reputation:

Your for-loop says that i < sentence.length() is the end-condition. This results it in it always being true and never accessing your for-loop because you declared i to be sentence.length() - 1. This is always going to be smaller than sentence.length().

Upvotes: 0

cigien
cigien

Reputation: 60208

Your reversedSentence string is empty, so indexing into it invokes undefined behavior. Instead, you can use push_back like this:

for (int i = sentence.length() - 1; i >= 0; i--)
{
    reversedSentence.push_back(sentence[i]);
}

Also note that your loop condition needs to be modified. In case sentence is empty, you should static_cast the .length() to an int before subtracting by 1, like this:

for (int i = static_cast<int>(sentence.length()) - 1; i >= 0; i--)
{
    reversedSentence.push_back(sentence[i]);
}

You could also just use an algorithm for this:

reversedSentence = sentence;
std::reverse(reversedSentence.begin(), reversedSentence.end());

This avoids complications when the sentence string is empty.

Upvotes: 3

Related Questions