I have a C++ program that finds and erases any vowels in a giving string. The only problem is, it doesnt work and I can't find the reason why. I have to use 2 functions that removes all the vowels and another that determines if a character is a vowel and all of this should operate in a loop.
This is my code:
#include <iostream>
#include <string>
using namespace std;
bool isA_Vowel(string s);
string remov(string s);
int main()
{
string s;
string ans = "y";
while((ans == "Y") || (ans == "y"))
{
cout << "Please enter a word or a series of letters: ";
cin >> s;
cout << "Old: " << s << endl;
cout << "New: " << remov(s) << endl;
cout << "Would you like to go again? <y/n> ";
cin >> ans;
}
}
bool isA_Vowel (string s)
{
if (s == "a" || s == "e"|| s == "i" || s == "o" || s == "u" || s == "A"
|| s == "E" || s == "I" || s == "O" || s == "U")
{
return (true);
}
else
{
return (false);
}
}
string remov(string s)
{
for (unsigned int i = 0; i < s.length(); ++i)
{
if (isA_Vowel(s))
{
s.erase(i,1);
}
}
return(s);
}
I had it working before, but now it won't run properly and erase all the vowels. Any suggestions or tips would be awesome! Thank you in advance!
Alright well I'm impantient and in a good mood, so here.
#include <iostream>
#include <string>
bool isVowel(char ch);
void removeVowels(std::string& str);
int main()
{
std::string Text = "";
std::cout << "Please enter a string, what ever you like: ";
std::getline(std::cin, Text);
removeVowels(Text);
std::cout << Text << std::endl;
return 0;
}
bool isVowel(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;
}
}
void removeVowels(std::string& str)
{
int len = str.length();
int index = 0;
while (index < len)
{
if (isVowel(str[index]))
{
str = str.substr(0, index) + str.substr(index + 1, str.length());
len = str.length();
}
else index++;
}
}
The issue you were having was
I had it working before, but now it won't run properly and erase all the vowels. Any suggestions or tips would be awesome! Thank you in advance!
In your for loop don't use an unsigned int, just use an int!
Generally speaking you shouldn't really just return true or false "straight" as you've done here, there are a variety of reasons why, and I'm sure your professor will cover them. But for all intents and purposes declare a boolean variable (bool vowel = true) is an example of one and you can use that in your return.
Because you're using an if statement on it's own with no looping structure (and even with one you'd still have issues) it's only executing once which means at best it would only find one vowel. You're also returning true but you're not providing logic to handle it.
For instance what do you want to occur when it returns true, and what do you want it to do when it returns false?
You're using cin which does not and will not take multiples words (white spaces) use getline(cin, input); (input being your input variable)
I actually misread your code and I've changed my comment here to clarify. You say in your function to remove the vowel s.erase(i, 1); essentially what you're doing is starting at position 0 in your string iterating forward by 1 position and deleting the starting and ending point (starting point being 0 ending point being 1).
Remember the first position is 0 not 1.
If you've got questions about the code I provided you please let me know and I'll explain it to you!