vowel counting problem
Hey everyone,
we're going over functions now and I am supposed to write a program that counts the number of vowels in string of characters. I'm not sure what I'm doing wrong... could someone help?
<pre><code>#include <iostream>
using namespace std ;
int isVowel ( string ) ;
int main()
{
int y ;
y = isVowel ("Enter a string of characters followed by a period: ") ;
cout << y ;
}
int isVowel ( string msg )
{
char ch ;
cout << msg ;
while ( ch != '.' )
{
cin >> ch ;
if ((ch == 'a') || (ch == 'e') || (ch == 'i') || (ch == 'o') || (ch == 'u'))
vowel++ ;
}
return vowel ;
}</code></pre>
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
For someone with almost 400 posts, you should have known better to explain what is going wrong (like post error messages), rather than just ask for help. If you compiled this program, you would have found that the variable vowel is not defined. Define it, initialize it to 0 , and the program should work.
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
you forgot to include header file.
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
For someone with almost 400 posts, you should have known better to explain what is going wrong (like post error messages), rather than just ask for help. If you compiled this program, you would have found that the variable vowel is not defined. Define it, initialize it to 0 , and the program should work.
Thanks for the reply
sorry for not explaining my code, my mistake. I should have known better, you're right.
When I compile it, at line 20 I get the following error:error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
What am I doing wrong?
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
You may need to use preposser commands like :
#include
using namespace std;
Line 11 : You're sending a literal string to itsVowel, not a string entered by the user. In fact there is no way for a user to enter a string in this program.
Line 22: The string sent to isVowel() doesn't have a period at the end.
Line 24: This asks user to input a char. This is wrong. You want to access each char of the string input by the user.
Line 26: You're only counting lower case vowels, not upper case vowels, which are also vowels.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
You may need to use preposser commands like :
#include
using namespace std;
Line 11 : You're sending a literal string to itsVowel, not a string entered by the user. In fact there is no way for a user to enter a string in this program.
Line 22: The string sent to isVowel() doesn't have a period at the end.
Line 24: This asks user to input a char. This is wrong. You want to access each char of the string input by the user.
Line 26: You're only counting lower case vowels, not upper case vowels, which are also vowels.
Thanks Drag and Lerner
Line 11, 22, 26: These work fine in the program tests, though I may be missing your point. Could you elaborate?
Line 26: Good point. though if I put ch = tolower(ch) after cin >> ch ; i get an infinite loop =o
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
When I compile it, at line 20 I get the following error:
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
What am I doing wrong?
you missed my post -- read it again and you will find the answer.
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
you missed my post -- read it again and you will find the answer.
you missed my post as well ;)
thanks!
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
I assume you want user to enter a sentence to be evaluated for vowels and send that entire sentence to the function. Your program doesn't do that. Your program excepts input char by char. To input a sentence the user would have to push the enter key after each letter they typed. I'm almost postive that isn't what you want.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
you missed my post as well ;)
thanks!
yup, sure did. That's what happens when several people post at nearly the same time.
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I assume you want user to enter a sentence to be evaluated for vowels and send that entire sentence to the function. Your program doesn't do that. Your program excepts input char by char. To input a sentence the user would have to push the enter key after each letter they typed. I'm almost postive that isn't what you want.
Lerner, thank you for your replies. I'm not getting the errors you're describing. After declaring 'value' and including the program looks to be working fine.
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
is there a better way to write my program?
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
Why restrict the user by asking him to enter a "period" after the sentence ? Replace your while loop with a for loop, something like this:
int length = str.length ();
for (int i = 0; i < length; ++i)
{
// do processing
}
Accept the whole sentence from the user in the form of a string and pass the string to this function, which will loop through each character and find the vowel count.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Why restrict the user by asking him to enter a "period" after the sentence ? Replace your while loop with a for loop, something like this:
int length = str.length ();
for (int i = 0; i < length; ++i)
{
// do processing
}
Accept the whole sentence from the user in the form of a string and pass the string to this function, which will loop through each character and find the vowel count.
hmm, i haven't learned about str.length, but I will put it in there and maybe impress the prof ;) ty
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
hm, it says str undeclared identifier
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
>hm, it says str undeclared identifier
'str' is the name of your string. First of all put input into the string perhaps like this:
string line;
getline(cin, line);
And then use line.length() to find out how many chars the user entered.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
ok this is my code, but i keep getting 0 vowels.
<pre><code>int isVowel ( string msg )
{
char ch ;
int vowel ;
cout << msg ;
string line ;
getline ( cin, line ) ;
int length = line.length () ;
for (int i = 0; i < length; ++i)
//while ( ch != '.' ) //'.' delimeter
{
/*ch = tolower(ch) ;*/
if ((ch == 'a') || (ch == 'e') || (ch == 'i') || (ch == 'o') || (ch == 'u') || (ch == 'A') || (ch == 'E') || (ch == 'I') || (ch == 'O') || (ch == 'U') )
value++
}
return value ;
} //end 'isVowel' function</code></pre>
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
'ch' doesn't contain anything because you removed your input loop. Replace it with:
line[i]
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
Does the above function even compile? I see an undefined variable called value and a missing semicolon after value++ .
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
Looks like Duki intended it to be vowel++, not value++. But it's a mystery on how it compiled...
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339