I'm a novice programmer currently learning how to use C++. I'm trying to complete a challenge on CodeWars. The program is supposed to accept a string input, and remove all of the vowels contained in the string.
First, I created a character array containing the vowels in lowercase and uppercase. Then I used the std::find function to search the input. What I wanted to happen was: If it was able to find the current character in the array, it would erase the character, and start the loop over again. It was able to isolate the vowels, but when I try to return the modified string, I'm met with an out_of_range of memory location error.
I still don't quite understand how memory works, so I'd appreciate some help.
#include <string>
#include <iostream>
#include <conio.h>
#include <algorithm>
using namespace std;
string disemvowel(string str)
{
char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
char *finder;
for (int i = 0; i < str.length(); i++)
{
char active = str[i];
finder = find(vowels, vowels + 10, active);
if (finder != vowels + 10)
{
str.erase(str[i], 0);
}
}
return str;
}
int main() {
string str;
cout << "say something \n";
cin >> str;
cout << disemvowel(str);
_getch();
return 0;
}
Thanks for the help