Write your question here.
i am suppose to right a loop that finds and remove the vowels of a sting i cannot make it work. My program only removes the first letter. i have not added all the vowels yet i wanted to get it to work with just an 'a' first .
#include <iostream>
#include <string>
usingnamespace std;
string removeVowel(string word)
{
int length = word.length();
for (int counter = 0; length > counter; counter++)
{
if (word.at(counter) == 'a')
{
word.erase(counter, 1);
cout << word;
}
}
return 0;
}
int main()
{
string fruit = "banana";
cout << removeVowel(fruit);
}
Output:
bnanabnnabnnterminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::at
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Several issues:
• after using the erase() function, the length of the string will change, but the variable int length contains the original unchanged value.
• the function promised to return a string but instead returns zero.
• there is a cout (presumably for debugging) which clutters the result.