Step Six  Vowels

Design this program called vowel.cpp to accept the input of one character value and output whether it is a vowel or a consonant. Use the switch statement to decide which is which. To make things simple, let's assume there are only 10 vowels--UPPER and LOWER case a,e,i,o,u!


switch (letter) {
  case 'A':
  case 'e':
  ...
    cout << "vowel!" << endl;
    break;
  default:
    cout << "consonant!" << endl;
}
instead of:
if (letter == 'A' || letter == 'e' ... ) {
  cout << "vowel!" << endl;
}
else {
  cout << "consonant!" << endl;
}

It may be more exciting to count the number of vowels in an entire text file. Let's say you have a text file called "gettysburg.txt" which contains the Gettysburg Address. You could do that something like this:


int vowelcounter = 0;
int consonantcounter = 0;
while (cin.get(letter)) {
  switch (letter) {
    case 'A':
    ...
      ++vowelcounter;
      break;
  default:
    ++consonantcounter;
  }
  //now display the results
}

Then run the program like this, so that input comes from the file gettysburg.txt rather than from you and the keyboard (this is an example of Input/Output redirection or I/O redirection):

./vowel < gettysburg.txt

This program will actually count too many consonants because it will count spaces, periods, commas, etc as consonants. We will fix that when we get to the alpha-only program.

Advance to Step Seven

Return to the Table of Contents