-
Well, I got it to compile, FINALLY!!! but I have 1244576 vowels in "test"
I think I may have something wrong, Ya THINK!
I have to make a funny, or I am going to cry!!
I think I am getting a little punchy, I've been at this since 4am, and it is now 7pm!!!
I have to finish this, it is an online class, where the "instructor" is non existant, and I have a deadline to complete the class - December 18th. With 390 pages of text, 15 programs and 3 more test, I don't think I will make it. I was hoping to finish this last night and the next 5 programs today after reading the covered chapters. I think I am CRAZY!!!
-
The latest and not so greatest!!!
Code:
#include <iostream>
#include <string>
using namespace std;
void displayBanner();
int vowels(string phrase);
int main()
{
int ptr = 0;
displayBanner();
cout << "Enter a phrase: ";
string phrase;
getline(cin, phrase);
cout << "\nThe phrase you entered: " << phrase << endl;
cout << "\nhas " << vowels(phrase) << " vowels." << endl;
return 0;
}
void displayBanner()
{
cout << "This programs asks for you to enter a string.\n"
<< "it then calculates how many vowels it contains\n"
<< "using a recursive function.\n\n";
}
int vowels(string phrase)
{
if (phrase.empty())
return 0;
if (phrase[0] == 'a'
|| phrase[0] == 'e'
|| phrase[0] == 'i'
|| phrase[0] == 'o'
|| phrase[0] == 'u'
|| phrase[0] == 'A'
|| phrase[0] == 'E'
|| phrase[0] == 'I'
|| phrase[0] == 'O'
|| phrase[0] == 'U')
return vowels(phrase.erase(0, 1));
}
I looked up str.erase() as you suggested, and srt.erase(m, n) "starting at index m, removes the next n characters from str." So, that is what I used and Boo Hoo (I'm sorry,I made a funny so I wouldn't cry, my bad), I was wrong. But, I am not surprised.
On the bright side, if there is one, the empty string works JUST FINE! HA, I got one right.
What is a Deathray Engineer? Do they work, the dethrays I mean? Can I borrow it for tonight? I won't have to worry about recursion anymore!
-
I've changed my last return statement to "return vowels(phrase) + vowels(phrase.erase(0, 1));"
and I still get the wierd over-the-top, way too many number of vowels, so is there something else missing that I don't see or haven't thought of?
And, if I enter a string without any vowels, it returns the same &$*#^*! number!
What is going on?
There has to be something really, really small that is missing in all of this!
-
Two comments
1. Usually it is an if/else with recursion
Code:
int vowels(string phrase)
{
if (phrase.empty())
return 0;
else if (phrase[0] == 'a'
|| phrase[0] == 'e'
|| phrase[0] == 'i'
|| phrase[0] == 'o'
|| phrase[0] == 'u'
|| phrase[0] == 'A'
|| phrase[0] == 'E'
|| phrase[0] == 'I'
|| phrase[0] == 'O'
|| phrase[0] == 'U')
return vowels(phrase.erase(0, 1));
}
2. You can shorten this by using toupper() to shorten the or's ...
3. I would have said....
Code:
if (phrase[0] == "")
return 0;
else
....
-
Thanks, I changed my code to if else after the last post, but that didn't change the aberrant number issue.
And, I thought that once I got the function to work I would use the toupper() or tolower() to shorten the code up. But I can't figure out the bogus number issue.
Any ideas?
I've been piddling with the function and the same number shows it's ugly head every time!
I just included a cout statement on "phrase" between my vowel check and the final return "test" prints every time and a lot, so it is obvious that my phrase string less the first character isn't doing wht I was expecting. So, now I atleast know where to "fix" my bug.
No that was not the problem.
-
Why don't you try getting to work non- recursively first- then rewrite using recursion?
-
Here is the start of a non-recursive sol'n
Code:
int vowel = 0; /* counts the total vowels */
int length; /* length of str */
for ( index = 0; index < length; index++ ) {
if ( str[index] == 'a' || ... ) {
vowel = vowel + 1;
}
}
-
I WILL try that. Thanks!
Hopefully that will give me some answers becasue I am not getting far this way.
-
That was fast easy and right on! I can do that, that is the way I think!
This is the function as an iterative (and it works):
Code:
int vowels(string phrase)
{
int vowel = 0;
if (phrase.empty())
return 0;
for ( int index = 0; index < phrase.length(); index++)
{
if(phrase[index] == 'a'
|| phrase[index] == 'e'
|| phrase[index] == 'i'
|| phrase[index] == 'o'
|| phrase[index] == 'u'
|| phrase[index] == 'A'
|| phrase[index] == 'E'
|| phrase[index] == 'I'
|| phrase[index] == 'O'
|| phrase[index] == 'U')
vowel += 1;
}
return vowel;
}
Now for the translation!
-
The base case involves the empty string, which contains zero vowels. For other cases, examine the first letter and add one, if necessary, to the result of applying the procedure to the substring consisting of all letters except the first.
-
I will see what I can come up with but this is where I fall down, hard!
-
Functionally they are the same, but you may find a switch/case structure easier to write (and possibly read) than the if statement with many conditions ORed together:
Code:
switch (phrase[index])
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
++vowel;
}
Also, it probably does not make much of a difference, but we typically pass objects of class types by reference. In this case, the string should be passed by const reference since it is not modified:
Code:
int vowels(const string& phrase)
{
// ...
Another thing: again, it does not make a real difference here, but the length() member function of std::string returns a std::string::size_type, not an int. By default, std::string::size_type is defined to a type that resolves to size_t, and size_t in turn is typically defined as unsigned int. As such, it is better to write:
Code:
for ( string::size_type index = 0; index < phrase.length(); index++)
-
He means something like...
Code:
numChars = str.length()
for (i = 0; i < numChars; i++)
{
switch(str.at(i))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowelcount++:
}
}
-
Thanks laserlight, for all the info.
I am trying to create a recursive function to count the vowels, but couldn't get it to work, so Mister C suggested I write the function iteratively and then translate. I got the iterative function to work but now i am back to struggling with figuring out what the recursive return call is to the function after the vowel check.
Any suggestions?
-
This is what I have for my function as a recursive one:
Code:
int vowels(string phrase)
{
if (phrase.empty())
return 0;
else if (phrase[0] == 'a'
|| phrase[0] == 'e'
|| phrase[0] == 'i'
|| phrase[0] == 'o'
|| phrase[0] == 'u'
|| phrase[0] == 'A'
|| phrase[0] == 'E'
|| phrase[0] == 'I'
|| phrase[0] == 'O'
|| phrase[0] == 'U')
return vowels(phrase) + vowels(phrase.substr(1, phrase.size()));
}
And I am still getting a huge, not close to being right number of vowels returned to main. 1244576 vowels! for the word "test"
however, when I change the phrase to pass by constant reference, I am down to 116 vowels contained in "test" so I am getting closer!