Thread: Help with removing characters

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    7

    Help with removing characters

    I am writing a program that will get a string from the user and remove all vowels within the string, as long as the first letter of any word in the string is not a vowel.
    Im required to use switch, getline, and if functions. I think Im pretty close; my code compiles but doesnt remove the vowels like I thought it would. Anyone wanna help a student??
    Thanks
    Code:
    #include <string>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    enum ConversionType {A,E,I,O,U};
    string word;
    int test = 0;
    void sw(){
    switch(::test) {
    	case A:{ //if the first letter of the string is 'a', remove all other vowels
    	word.erase(remove(word.begin(), word.end(), 'e'), word.end());
    	word.erase(remove(word.begin(), word.end(), 'i'), word.end());
    	word.erase(remove(word.begin(), word.end(), 'o'), word.end());
    	word.erase(remove(word.begin(), word.end(), 'u'), word.end());
    	word.erase(remove(word.begin(), word.end(), 'y'), word.end());
    	break;
    }
    	case E:{//if the first letter of the string is 'e', remove all other vowels
    	word.erase(remove(word.begin(), word.end(), 'a'), word.end());
    	word.erase(remove(word.begin(), word.end(), 'i'), word.end());
    	word.erase(remove(word.begin(), word.end(), 'o'), word.end());
    	word.erase(remove(word.begin(), word.end(), 'u'), word.end());
    	word.erase(remove(word.begin(), word.end(), 'y'), word.end());
    	break;
    	}
    	case I:{//if the first letter of the string is 'i', remove all other vowels
    	word.erase(remove(word.begin(), word.end(), 'a'), word.end());
    	word.erase(remove(word.begin(), word.end(), 'e'), word.end());
    	word.erase(remove(word.begin(), word.end(), 'o'), word.end());
    	word.erase(remove(word.begin(), word.end(), 'u'), word.end());
    	word.erase(remove(word.begin(), word.end(), 'y'), word.end());
    	break;
    	}
    	case O:{//if the first letter of the string is 'o', remove all other vowels
    	word.erase(remove(word.begin(), word.end(), 'a'), word.end());
    	word.erase(remove(word.begin(), word.end(), 'e'), word.end());
    	word.erase(remove(word.begin(), word.end(), 'i'), word.end());
    	word.erase(remove(word.begin(), word.end(), 'u'), word.end());
    	word.erase(remove(word.begin(), word.end(), 'y'), word.end());
    	break;
    	}
    	case U:{//if the first letter of the string is 'u', remove all other vowels
    	word.erase(remove(word.begin(), word.end(), 'a'), word.end());
    	word.erase(remove(word.begin(), word.end(), 'e'), word.end());
    	word.erase(remove(word.begin(), word.end(), 'i'), word.end());
    	word.erase(remove(word.begin(), word.end(), 'o'), word.end());
    	word.erase(remove(word.begin(), word.end(), 'y'), word.end());
    	break;
    	}
    	
    	}
    }
    int main()
    {
    	string word;
    	cout << "Please enter your ad: ";
    	getline (cin,word);
    	
    	if(word[0] == 'a') // check to see what the first character is
    		::test = A;
    	else if(word[0] == 'e')
    		::test = E;
    	else if(word[0] == 'i')
    		::test = I;
    	else if(word[0] == 'o')
    		::test = O;
    	else if(word[0] == 'u')
    		::test = U;
    	cout << word << '\n';
    }

  2. #2
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    Your main() doesnt do anything but ask for input, set an enumeration, and output the input. Also, you forgot to explicitly return main().

    Im sorry, i can't help you otherwise because im not familiar with the pre-defined functions / classes you used. I think it's better to use as little as possible pre-defined things when coding as a learning exercise.
    I AM WINNER!!!1!111oneoneomne

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    How would I get the main() to go through the switch construct?
    You're right, my output is the same as my input each time.
    Thanks for the help.

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I don't quite understand your function, but a couple of problems that I noticed. First off, your switch doesn't take into account a word that starts with a consonant, or do you only take vowels away from words that start with vowels? Second, if the word starts with the letter 'a', don't you want to take away all the a's in the word EXCEPT for the first one? Your program doesn't take that into account either. Last, your program only checks the first letter in the first word of the string, not the first letter of each word.

    To help you out, an easier way would be to simply pass the whole word into the function, and then remove ALL vowels starting with the second letter. Since I assume your string will have more than one word in it, you'll have to parse the string using a loop though. Let me know if this helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Removing Specific Characters from Strings
    By gfmartin05 in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2009, 09:53 AM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. special characters removing from srting
    By cnu_sree in forum C Programming
    Replies: 5
    Last Post: 06-06-2007, 08:30 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. removing characters from strings
    By Geolingo in forum C++ Programming
    Replies: 1
    Last Post: 04-27-2003, 09:33 PM