Hey guys. I coded the following program below: but I have a problem... I think its perhaps the logic I have used is not too good... here it goes...
The program below should: ask the user to enter a letter ('a' to 'z' or 'A' to 'Z') and outputs VOWEL or CONSONANT depending upon the input. You should use a switch statement to identify the vowels and cascade these cases togther. If the user types in a character which is not a letter you should print out the message NOT-A-LETTER.

I get the program to do all of these but when I execute it and say I type in A the output I get is A is a Vowel and then A is a CONSONANT as well<< its this extra bit which is my problem... It shows that all the Vowels are vowels but are also Consonants which dose not make sense.
Here is the c code:

#include <stdio.h>
#include "simpleio.h"

int main() // This program has the BOTH feature added to it
{
char letter;

printf("Please enter a letter 'A'-'Z' or 'a'-'z' : \n");
letter = getchar();

if ((letter >= 'A' && letter <= 'Z' ) || (letter >='a' && letter <= 'z'))
{
printf(" %c is a CONSONANT\n", letter);
}

else
{
printf("NOT-A-LETTER\n");
}

switch(letter)
{
case 'A' : printf(" A is a vowel\n"); break;
case 'a' : printf(" a is a vowel\n"); break;

case 'E' : printf(" E is a vowel\n"); break;
case 'e' : printf(" e is a vowel\n"); break;

case 'I' : printf(" I is a vowel\n"); break;
case 'i' : printf(" i is a vowel\n"); break;

case 'O' : printf(" O is a vowel\n"); break;
case 'o' : printf(" o is a vowel\n"); break;

case 'U' : printf(" U is a vowel\n"); break;
case 'u' : printf(" u is a vowel\n"); break;

case 'Y' : printf(" Y is BOTH\n "); break;
case 'y' : printf(" y is BOTH\n "); break;

}

return 0;
}

many thanks hope you can help me out.

Recommended Answers

All 6 Replies

Make it so that your if statement looks for the vowels, then have else if look for the consonants (hint: all the vowels will be filtered out by the first stage so you don't have to be so fussy about the ranges), then have else determine not-a-letter status.

if (get the vowels)

else if (get the consonants)

else ( not a letters automatically here)

Draw it out on paper if you are still having trouble.

if ((letter >= 'A' && letter <= 'Z' ) || (letter >='a' && letter <= 'z'))
{
  printf(" %c is a CONSONANT\n", letter);
}

If it's a letter at all, you always say it's a consonant. If you separate error checking from processing, you can eliminate that problem:

if ( isalpha ( letter ) )
{
  switch ( toupper ( letter ) )
  {
  case 'A': case 'E': case 'I': case 'O': case 'U':
    printf ( "%c is a vowel\n", letter );
    break;
  default:
    printf ( "%c is a consonant\n", letter );
    break;
  }
}
else
{
  printf ( "Not a letter\n" );
}
commented: You are a true poetess of the source code! +2

I think you guys have solved this and I am very thankful :) I now understand why which I will try out thanks alot guyZ!

Hi
As you have said, it is the logic you have used. There is a slight modification I have made in your program and it will show a vowel only as a vowel. Here it is:

#include <stdio.h>
#include "simpleio.h"

int main() // This program has the BOTH feature added to it
{
char letter;

printf("Please enter a letter 'A'-'Z' or 'a'-'z' : \n");
letter = getchar();

if ((letter >= 'A' && letter <= 'Z' ) || (letter >='a' && letter <= 'z'))
{
switch(letter)
{
case 'A' : printf(" A is a vowel\n"); break;
case 'a' : printf(" a is a vowel\n"); break;

case 'E' : printf(" E is a vowel\n"); break;
case 'e' : printf(" e is a vowel\n"); break;

case 'I' : printf(" I is a vowel\n"); break;
case 'i' : printf(" i is a vowel\n"); break;

case 'O' : printf(" O is a vowel\n"); break;
case 'o' : printf(" o is a vowel\n"); break;

case 'U' : printf(" U is a vowel\n"); break;
case 'u' : printf(" u is a vowel\n"); break;

case 'Y' : printf(" Y is BOTH\n "); break;
case 'y' : printf(" y is BOTH\n "); break;

default: printf("The letter is a consonant");
}
else
{
printf("NOT-A-LETTER\n");
}

return 0;
}

Hi
As you have said, it is the logic you have used. There is a slight modification I have made in your program and it will show a vowel only as a vowel. Here it is:

Did you not understand the logic behind using isalpha and toupper, or did you just remove them to make your code possibly less correct and likely less efficient? And why all of the duplicate printfs? Again avoiding simplicity in favor of unnecessary addition of redundancy?

Pls. Help me to make a program the shows if a letter is a vowel or consonants using if and else if

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.