Removing Vowels

The goal is to remove vowels. Could someone help me with the string.at part... clearly my syntax is wrong. Thanks!


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

void vowelremover(string&);
string word;
int main ()
{

cout << "Enter a word of which you want the vowels removed: ";
cin >> word;

vowelremover(word);

cout << "The word without vowels is: " << word << endl;
return 0;

}



void vowelremover (string& enteredword)
{
int posvowel;
int i;

for (i == 0; i < enteredword.length(), i ++)
{
while (enteredword.at(i) = 'a'; || enteredword.at(i) = 'e'; || enteredword.at(i) = 'i'; || enteredword.at(i) = 'o'; ||
enteredword.at(i) = 'u'; || enteredword.at(i) = 'A'; || enteredword.at(i) = 'E'; || enteredword.at(i) = 'I'; || enteredword.at(i) = 'O'; ||
enteredword.at(i) = 'U';)

{
enteredword.erase(i, 1);
i == 0;
}

}


Hi PoolOfDeath, there are several mistakes. For the for loop, i < enteredword.length(),, it should be i < enteredword.length();.

Second, for the while condition, it should be enterword.at(i)=='a', with '==' and no';'.

Hope these help. :)
i had the enterword.at(i)='a' but it says 'a' has to be a modifiable lvalue so it doesnt work. any idea on that?
Last edited on
so now i'm getting the error "Unhandled exception at 0x770b15de in VowelDeleter.exe: Microsoft C++ exception: std:)out_of_range at memory location 0x003ef9c4.."
hello
The reason you are getting that error is because you are trying to access the memory which is not there. I suspect after you erase the alphabet, you will need to update enteredword.length() again.

I made a few changes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void vowelremover (string& enteredword)
{
	//int posvowel;	//What is this for???
	int i = 0;
	int length = int(enteredword.length());
	while (i < length)
	{
		if (enteredword.at(i) == 'a' || 
		    enteredword.at(i) == 'e' || 
		    enteredword.at(i) == 'i' || 
		    enteredword.at(i) == 'o' ||
		    enteredword.at(i) == 'u' || 
		    enteredword.at(i) == 'A' || 
		    enteredword.at(i) == 'E' || 
		    enteredword.at(i) == 'I' || 
		    enteredword.at(i) == 'O' ||
		    enteredword.at(i) == 'U')
		{
			enteredword.erase(i, 1);
			length = int(enteredword.length());
		}
		else i++; //not a vowel, check the next letter
	}
}


Hope it is what you want :)
Topic archived. No new replies allowed.