简体   繁体   中英

Writing a prototype function(c++)

I have to write the prototype and the implementation of a C++ function that receives a character and returns true if the character is a vowel and false otherwise. Vowels include uppercase and lowercase of the following characters 'a'. 'e', 'i', 'o' and 'u'.

I have written

bool vowelOrNot(char x)
{   if(x="a" or "e" or "i" or "o" or "u")
       cout<<"true"<<endl;

    else
       cout<<"false""<<endl;
}

i wrote or cause i dont know how to do the lines on here, am i correct on my function?

As no one suggested it, here is a solution using a switch statement:

bool vowelOrNot(char x)
{
    switch (x)
    {
        case 'a':
        case 'A':
        case 'e':
        case 'E':
        case 'i':
        case 'I':
        case 'o':
        case 'O':
        case 'u':
        case 'U':
            return true;

        default:
            return false;
    }
}

I considered using toupper to convert input and only check capitals in the case.

You will need a test, eg,

int
main ( int argc, char *argv[] )
{
   bool test1 = vowelOrNot ( 'a' );
   std::cout << test1 << " expected to be true" << std::endl;

   return test1 == true ? EXIT_SUCCESS : EXIT_FAILURE;    
}

Of course, the test isn't complete. But you have to write the test for all possible input data.

bool vowelOrNot(char x) //x must be lowercase for the function to work as expected
{   if(x=='a' || x=='e' || x=='i' || x=='o' ||  x=='u' ) //== for comparing and single quotes for a char.
   //|| is the logical OR
   {
       cout<<"true"<<endl;
       return true; //return true to function caller
   }
    else
       cout<<"false"<<endl;
   return false;//return false to function caller
}

Be careful with the use of the word prototype. A C++ function prototype is a declaration that usually occurs at the top of a file before main() or in a header file of a module (probably the former in your case). It would look like this:

bool vowelOrNot(char);

What you have is the implementation, but you have incorrect syntax. "or" is not a keyword in C++. Use "||". Also, "==" is the equal-to comparison operator rather than "=". I recommend reviewing at least the following documentation: http://www.cplusplus.com/doc/tutorial/control/ .

Also, I noticed that your function returns a boolean, yet you are printing the word for each boolean value rather than returning it. If you need to print those words, it should be handled elsewhere based on the return value of the function.

The solution I recommend is as follows:

#include <string>
#include <cctype>
using namespace std;

bool vowelOrNot(char);

const string VOWELS = "aeiou";

int main
{
    //some code that uses vowelOrNot, perhaps printing true and false
}

bool vowelOrNot(char c)
{
  return VOWELS.find(tolower(c)) != string::npos;
}

Finally, I recommend renaming the function to is_vowel() or something like that to be more clear and concise about the function's purpose.

Hope this helps!

Try this:

    bool vowelOrNot(char x)
    {   if(x=='a' || x=='e' || x=='i' || x=='o' || x=='u' || x=='A' || x=='E' || x=='I' || x=='O' || x=='U')
         {
           cout<<"true"<<endl;
           return true;
          }

        else
        {
           cout<<"false"<<endl;
           return false;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM