DomeWTF
DomeWTF

Reputation: 2420

terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::erase

string Farfallino::decode(string buff) {

string stringa;
size_t pos;

while(1) {
    while(pos = (buff.find("afa"))) {
        buff.erase(pos, 3);
        buff.insert(pos, "a");
    }
    while(pos = (buff.find("efe"))) {
        buff.erase(pos, 3);
        buff.insert(pos, "e");
    }
    while(pos = (buff.find("ifi"))) {
        buff.erase(pos, 3);
        buff.insert(pos, "i");
    }
    while(pos = (buff.find("ofo"))) {
        buff.erase(pos, 3);
        buff.insert(pos, "o");
    }
    while(pos = (buff.find("ufu"))) {
        buff.erase(pos, 3);
        buff.insert(pos, "u");
    }
}

return stringa;
}

I'm trying to erase every "afa" "efe" "ifi" "ofo" and "ufu" that are in the string passed to the function, but it gives me this error. I have no idea what i'm doing wrong..

Upvotes: 1

Views: 5674

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477338

It should be something like this:

while ((pos = buff.find("x")) != std::string::npos)
{
    // ...
}

"Not found" is signaled by returning npos, not zero. Zero would just be the first character.

Upvotes: 5

Related Questions