**It is my humble request if I can get minior help with function definition at the end where its says--- int countvowels (string test) { **

#include <iostream>
using namespace std;

//function prototype
bool is_vowel (char c);
//is_vowel(c) returns true if character c is a vowel and false otherwise
void testdrivera();
//returns the number of uppercase and lowercase vowel in string test
void testdriverb();
//returns the number of vowels
int countvowels (string test); // defines variable string
//count_vowels(test) returns the number of vowels in string(test)
//test driver for b
int main ()
``
    testdrivera ();
    return 0;
}

void testdrivera ()
{
    string test = "Programming language IS FUN to learn. ";
    for (int i = 0; i < test.size(); i++)
    {
        if (is_vowel(test [i]))
            cout << "The letter followed by is a vowel: " << test [i] << endl;
        else
            cout << "The letter followed by is NOT a vowel: " << test [i] << endl;
    }
}

void testdriveb()
{
    string test = "Programing language IS FUN to learn. ";
    int nvowels = 0;
    nvowels = countvowels (test);
    cout << "Number of vowels: " << nvowels << endl;
}

bool is_vowel (char c)
{
    switch (c)
    {
        case 'a': case 'A': case 'e': case 'E': case 'i': case 'I':
        case 'o': case 'O': case 'u': case 'U':
            return true;
    }
    return false;
}
int countvowels (string test)
{

    return 0;
}

Recommended Answers

All 3 Replies

You need to loop over every char in the string.
Subject each char to the function is_vowel.
Count how many times the function comes back as true.
Return that total.

int countvowels (string test)
{
    char  c, is_vowel ;
    int countvowels;
    bool result;

    cout << "Programing language IS FUN to learn: " << endl;
    cin.get(is_vowel);

    is_vowel = 0;
    while (is_vowel != '\n')

    {
        result = is_vowel(c);

        if (result)
            is_vowel ++;
        cin.get(is_vowel);
    }

    system("pause");
    return 0;
}

something like this? I'm sorry but I'm confuse :/

OK, you have some of the idea, but you seem confused about some aspects, such as parameteters and return values. Let me give some suggestions.

First off, your goal is to get the number of vowels in the function parameter, test. The parameter of a function is a value passed to it so that it can operate on that value. For example, in the function square() below,

double square(double x)
{
    return x * x;
}

the variable x is the formal parameter of square(). If I then have a function that calls square() with the argument 5,

n = square(5);

I would expect n to be assigned 25. I could also pass it a variable, like so:

m = square(n);

which would set m to 625. This is one of the main ideas behind functions: that you can use them with different arguments and get different results.

When testdriverb() calls countvowels() with the argument test (which has been set to the string "Programing language IS FUN to learn: "), it sets the value of the parameter test in countvowels() to that string.

Note that the name of the argument doesn't have to match the name of the parameter, and in fact the argument doesn't even have to be a variable at all (in most cases). If you look again at my earlier code, you'll see that I used a literal as the argument to square(), for example. You could have called the variable in testdriverb() something like foo and it would work just the same.

The other thing to note is that, because you are getting the value to work on from the argument, you don't need to ask for any value from the user. you can drop the parts in lines 7 and 8 completely.

You also don't want to have a variable named is_vowel, because you already have a function named is_vowel() and the names will conflict. The same with countvowels; rename the variable to just count and you'll do OK. You don't need c or result in this case either.

What you do need is the string index operator, which is just like the array index operator but for the string class. The main comparison in you loop should be:

if (is_vowel(test[i])
{
   count++;
}

where i is the index variable of a for() loop. You will want to loop as long as i is less than test.length().

Finally, for this function, the returned variable should be count.

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.