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?

#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 ;
}

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.

you forgot to include <string> header file.

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?

You may need to use preposser commands like :
#include <string>
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.

You may need to use preposser commands like :
#include <string>
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

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.

you missed my post -- read it again and you will find the answer.

you missed my post as well ;)

thanks!

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.

you missed my post as well ;)

thanks!

yup, sure did. That's what happens when several people post at nearly the same time.

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 <string> the program looks to be working fine.

is there a better way to write my program?

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.

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

hm, it says str undeclared identifier

>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.

ok this is my code, but i keep getting 0 vowels.

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

'ch' doesn't contain anything because you removed your input loop. Replace it with:

line[i]

Does the above function even compile? I see an undefined variable called value and a missing semicolon after value++ .

Looks like Duki intended it to be vowel++, not value++. But it's a mystery on how it compiled...

Something like this should make your professor happy:

for (int i = 0; i < length; ++i)
    {
        ch = tolower (line [i]);
        if (isalpha (ch) &&
             (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'))
        {
            ++vowelCount;
        }
    }

you don't need the check for isalph on line 4 -- it really servers no purpose.

Oh, I believe processing cycles should be saved whenever possible.

Consider the input: 4384023824039483043840aadf348304923840394sdffdfd8304382409243464565204 Might seem a bit nonsense, but still happens. Or worse, if this function is applied to each and every line of the data file which is full of numerals. It helps me in saving or not performing the redundant computations (that would be 5 comparisions in worst case) whenever possible at little or no extra cost.

But then again, a matter of perspective and style.

cool thanks! it's working great now! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.