For starters, I would turn your testing into a series of if..else if statements, including the final recusive call. Then you need and else case that returns false.
vmanes
Postaholic
2,015 posts since Aug 2007
Reputation Points: 1,283
Solved Threads: 242
Skill Endorsements: 6
Still missing parts.
Base case - when string being examined is empty.
The recusive call needs to be in an if..else, and it's return needs to be returned.
The return false needs to be in an else block.
vmanes
Postaholic
2,015 posts since Aug 2007
Reputation Points: 1,283
Solved Threads: 242
Skill Endorsements: 6
The thinking mainly is wrong, because, on recursive calls, if indeed a vowel is found, it will still return false, because of the last condition. I suggest you take an extra variable, if allowed, which will carry the information:
void containsVowel (string s, bool &isIt) {
if (s.empty()) return;
if (s[0]=='a' || s[0]=='e' || s[0]=='u' || s[0]=='o' || s[0]=='i') isIt=true;
containsVowel(s.substr(1, s.size()-1), isIt);
}
int main(){
string a="bcdghijklmnpqrst";
bool isIt=false;
containsVowel(a, isIt);
if (isIt) cout<<"True.\n";
else cout<<"Not true.\n";
return 0;
}
Let me explain a bit what I mean, regarding your function:
string a="bcdfghijklmnpqrst"
this string conains the vowel i.
when calling your function, eventually will return true from finding the vowel:
here are the recursive returns:
"t" return false
"st" return false
"rst" return false
"qrst" return false
"pqrst" return false
"npqrst" return false
"mnpqrst" return false
"lmnpqrst" return false
"klmnpqrst" return false
"jklmnpqrst" return false
"ijklmnpqrst" return true // we found a solution
"hijklmnpqrst" return false //here the return changes, because 'h' is not a vowel.
"ghijklmnpqrst" return false
"fghijklmnpqrst" return false
"dfghijklmnpqrst" return false
"cdfghijklmnpqrst" return false
"bcdfghijklmnpqrst" return false
So, even thou your word conains vowels, it will still return false.
Again, I don't know if your teacher allowes you to make it into a void, and take an extra value which will hold the information.
Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12
Question Answered as of 4 Months Ago by
vmanes,
T-Dogg3030,
Gonbe
and 3 others