c++ - Program that reads vowels and erases them won't run properly -
i have c++ program finds , erases vowels in giving string. problem is, doesnt work , can't find reason why. have use 2 functions removes vowels , determines if character vowel , of should operate in loop.
this 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 word or series of letters: "; cin >> s; cout << "old: " << s << endl; cout << "new: " << remov(s) << endl; cout << "would 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) { (unsigned int = 0; < s.length(); ++i) { if (isa_vowel(s)) { s.erase(i,1); } } return(s); }
i had working before, won't run , erase vowels. suggestions or tips awesome! thank in advance!
alright i'm impantient , in mood, here.
#include <iostream> #include <string> bool isvowel(char ch); void removevowels(std::string& str); int main() { std::string text = ""; std::cout << "please enter string, ever 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 having
i had working before, won't run , erase vowels. suggestions or tips awesome! thank in advance!
in loop don't use unsigned int, use int!
generally speaking shouldn't return true or false "straight" you've done here, there variety of reasons why, , i'm sure professor cover them. intents , purposes declare boolean variable (bool vowel = true) example of 1 , can use in return.
because you're using if statement on it's own no looping structure (and 1 you'd still have issues) it's executing once means @ best find 1 vowel. you're returning true you're not providing logic handle it.
for instance want occur when returns true, , want when returns false?
you're using cin not , not take multiples words (white spaces) use getline(cin, input); (input being input variable)
i misread code , i've changed comment here clarify. in function remove vowel s.erase(i, 1); you're doing starting @ position 0 in string iterating forward 1 position , deleting starting , ending point (starting point being 0 ending point being 1).
remember first position 0 not 1.
if you've got questions code provided please let me know , i'll explain you!
Comments
Post a Comment