this worked a few times and now its not :( anny help would be greatly appretiated

#include <iostream>
#include <string>

using namespace std;

string vowels(string str)
{
    string vow = "aeiouAEIOU";
    for(size_t i = 0; i <= str.length()+1; i++)
    {
        str.erase((str.find_first_of(vow)),1);
    }
    return str;
}

int main()
{
    string str;
    cout << "Enter a string of charecters." << endl;
    cin >> str;

    vowels(str);

    return 0;
}

Dani AI

Generated

The runtime message comes from letting string::erase be called with an invalid position. As noted, erase throws std::out_of_range if the position is past the end. In this case find_first_of returns string::npos when no vowel is found; passing that to erase causes the exception and the "terminate in a special way" behavior. Also note the original loop uses <= str.length()+1 while modifying the string, which both permits invalid indices and makes the loop logic brittle. 's followup comment shows the fix was simply a fresh perspective — below are safe, practical fixes.

A safe iterative approach (stop when no match) — update the string only while find_first_of returns a valid position:

string remove_vowels(string s) {
    const string vowels = "aeiouAEIOU";
    size_t pos = s.find_first_of(vowels);
    while (pos != string::npos) {
        s.erase(pos, 1);
        pos = s.find_first_of(vowels);
    }
    return s;
}

A more efficient, idiomatic approach is to use std::remove_if and then erase once (linear time, avoids repeated shifting):

#include <algorithm>

string remove_vowels_fast(string s) {
    const string vowels = "aeiouAEIOU";
    s.erase(std::remove_if(s.begin(), s.end(), [&](char c){
        return vowels.find(c) != string::npos;
    }), s.end());
    return s;
}

Other practical notes: use getline(cin, str) if you need spaces; assign the returned string (e.g. str = remove_vowels(str);) since the function returns the modified copy; prefer checking find_first_of for string::npos rather than catching exceptions; and prefer the remove_if pattern for long strings for better performance. For reference, see basic_string::find_first_of and basic_string::erase on cppreference: find_first_of and erase.

Recommended Answers

All 2 Replies

http://www.cplusplus.com/reference/string/string/erase/

pos
Position within the string of the first character to be erased.
If the position passed is past the end of the string, an out_of_range exception is thrown.

I see no checking for this condition and I see no catching of any exceptions, so if this is in fact occurring, it would certainly "terminate in a special way". Confirm that this doesn't happen or make sure you prevent and / or handle it.

some times all you need is a fresh prespective thanx for that i should have seen it along time ago

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.