Help for homework......isVowel function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • janice3
    New Member
    • Mar 2007
    • 7

    Help for homework......isVowel function

    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.

    Code:
    bool isVowel(char ch);
    int main
    {
       char letter;
       int num;
       cout<<"enter a sequence of characters: ";
       cin >> letters;
       letter=isVowel(letter);
       cout<<"the number of vowels are: "
       cin >> num;
    }
    
    bool isVowel(char ch)
    {
       char letter;
       if(letter=='a')
          return true;
       else if (letter =='e')
          return true;
       else if(letter=='i')
          return true;
       else if (letter =='o')
          return true;
       else if (letter =='u')
          return true;
       else
          return false;
    }
    Last edited by Ganon11; Mar 17 '07, 03:22 PM. Reason: code tags added, indented
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    cin >> letters is letters defined before ......

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

    welcome again

    Comment

    • Extremist
      New Member
      • Jan 2007
      • 94

      #3
      Why are you using this?:
      Code:
      cin >> num;
      You would do something like this:
      Code:
      bool isVowel(char ch)
      {
      
        if(ch =='a')
                 return 1;
        if(ch =='e')
                 return 1;
        if(ch =='i')
                 return 1;
        if(ch =='o')
                 return 1;
        if(ch =='u')
                 return 1;
        else
                return 0;
      }
      Iterate through each letter of the sequence which was input by the user
      and say something like:
      Code:
      if(isVowel(letters[x]))
           num++;
      Go look at how to declare letters, you didn't declare it right

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #4
        Or alternatively (I'll assume you first convert to lowercase, but if you don't the modification is trivial)....
        Code:
        int isVowel(char ch)
        {
          switch(ch)
          {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
              return 1;
            default:
              return 0;
          }
        }
        and you can call it by
        Code:
        num = num + isVowel(letters[x]);

        Comment

        Working...