By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Help for homework......isVowel function

P: 7
write a program that prompts the user to input a sequence of characters and outputs the number of vowels. using the function isVowel.
help me for code.

Expand|Select|Wrap|Line Numbers
  1. bool isVowel(char ch);
  2. int main
  3. {
  4.    char letter;
  5.    int num;
  6.    cout<<"enter a sequence of characters: ";
  7.    cin >> letters;
  8.    letter=isVowel(letter);
  9.    cout<<"the number of vowels are: "
  10.    cin >> num;
  11. }
  12.  
  13. bool isVowel(char ch)
  14. {
  15.    char letter;
  16.    if(letter=='a')
  17.       return true;
  18.    else if (letter =='e')
  19.       return true;
  20.    else if(letter=='i')
  21.       return true;
  22.    else if (letter =='o')
  23.       return true;
  24.    else if (letter =='u')
  25.       return true;
  26.    else
  27.       return false;
  28. }
Mar 17 '07 #1
Share this Question
Share on Google+
3 Replies


dmjpro
100+
P: 2,476
cin >> letters is letters defined before ......

i think u better to use gets instead of cin>> ....

welcome again
Mar 17 '07 #2

P: 94
Why are you using this?:
Expand|Select|Wrap|Line Numbers
  1. cin >> num;
  2.  
You would do something like this:
Expand|Select|Wrap|Line Numbers
  1. bool isVowel(char ch)
  2. {
  3.  
  4.   if(ch =='a')
  5.            return 1;
  6.   if(ch =='e')
  7.            return 1;
  8.   if(ch =='i')
  9.            return 1;
  10.   if(ch =='o')
  11.            return 1;
  12.   if(ch =='u')
  13.            return 1;
  14.   else
  15.           return 0;
  16. }
  17.  
Iterate through each letter of the sequence which was input by the user
and say something like:
Expand|Select|Wrap|Line Numbers
  1. if(isVowel(letters[x]))
  2.      num++;
  3.  
Go look at how to declare letters, you didn't declare it right
Mar 17 '07 #3

DeMan
100+
P: 1,806
Or alternatively (I'll assume you first convert to lowercase, but if you don't the modification is trivial)....
Expand|Select|Wrap|Line Numbers
  1. int isVowel(char ch)
  2. {
  3.   switch(ch)
  4.   {
  5.     case 'a':
  6.     case 'e':
  7.     case 'i':
  8.     case 'o':
  9.     case 'u':
  10.       return 1;
  11.     default:
  12.       return 0;
  13.   }
  14. }
  15.  
and you can call it by
Expand|Select|Wrap|Line Numbers
  1. num = num + isVowel(letters[x]);
  2.  
Mar 18 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.