0

I don't understand the problem here. I've researched it, it compiles fine but when I run the program it gives me the "Debug Assertion Failed!" error and the above explanation.

#include <iostream>
#include <string>
using namespace std;


bool checkVowel(char ch)
{
 switch(ch)
 {
     case 'a':
     case 'A':
     case 'e':
     case 'E':
     case 'i':
     case 'I':
     case 'o':
     case 'O':
     case 'u':
     case 'U':
          return true;
     default:
          return false;
 }}
int main()
{
string str;
char ch;
cout<<"Please enter a string, all vowels will be removed: ";
cin >> str;

for (int i=0;i=str.length();i++)
{

 if (checkVowel(str[i]))
     {
        str=str.erase(i);
 }}

cout << str;
}
1
  • Be very careful if you modify an object while you are traversing it. Be aware that your loop skips a character for each one it erases -- it moves on to the next position, skipping the character that 'slid' into the current due to the 'erase' operation. (This is a poor candidate for an if loop. Use while and don't increment i if you call erase.) Commented Nov 14, 2011 at 6:09

2 Answers 2

4

One error is here:

i=str.length()

should be:

i < str.length()

In your initial code, i=str.length() will always return true when the string is not-empty. So the effect is that you will be overrunning the string.

Furthermore, you don't want to increment the index when you do find a vowel, or you will skip the next character:

for (int i = 0; i < str.length(); )
{
    if (checkVowel(str[i]))
    {
        str.erase(i,1);
    }else{
        i++;
    }
}

Last thing: str=str.erase(i); is not necessary, just str.erase(i,1); is enough. (You'll need the second parameter as 1 as pointed out in the comments.)

Sign up to request clarification or add additional context in comments.

1 Comment

If you give string::erase a single numeric parameter it will erase to the end of the string. He should use str.erase(i,1);
1

for condition is wrong , it should be for (int i=0;i <= str.length();i++)

Alternatively you can use STL remove_if

remove_if(str.begin(), str.end(), checkVowel);

Complete program will be.

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool checkVowel(char ch){
    switch(ch){
        case 'a':
        case 'A':
        case 'e':
        case 'E':
        case 'i':
        case 'I':
        case 'o':
        case 'O':
        case 'u':
        case 'U':
            return true;
        default:
            return false;
    }
}
int main(){
    string str;
    char ch;
    cout << "Please enter a string, all vowels will be removed: ";
    cin >> str;
    remove_if(str.begin(), str.end(), checkVowel);
    cout << str;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.