Vowel or consonant what if user inputs a number?

In a vowel check program whether user inputs a vowel or consonant what if a user inputs a number and why it shows the message of consonant?how can i show the message"you have entered wrong input" kindly help i am new

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  #include <iostream.h>
  #include <conio.h>
int main(){
  char alpha;
  clrscr();
  cout<<"Enter an alphabet: ";
  cin>>alpha;
  if(alpha=='a'||alpha=='A'||alpha=='e'||alpha=='E'||alpha=='i'||alpha=='I'||alpha=='o'||alpha=='O'||alpha=='u'||alpha=='U')
      cout<<alpha<<"is a Vowel;
  else
      cout<<alpha<<"consonant";
     getch();
  return 0;
} 
why it shows the message of consonant?


Because you check only for two possibilities. If it is not a vowel than it is a consonant.

To solve the problem you need to check for a third option - i.e. an illegal input.
The logic to do is actually not so difficult.
In pseudo code you would write it like this:

1
2
3
4
5
6
If input is vowel then
  print "Is vowel"
Else if input is consonant then
  print "Is consonant"
Else
  print "illegal input"


I hope you can fix your code, otherwise post again.
Last edited on
i have tried this when i enter 1 it shows consonant i dont know why this is happening?
Check that the character is alphabetic isalpha(). Possibly convert to lowercase before checking it is a vowel.
See
http://www.cplusplus.com/reference/cctype/isalpha/
http://www.cplusplus.com/reference/cctype/tolower/

Other approaches, list all the valid characters in a string and test whether the character is found within that string
http://www.cplusplus.com/reference/string/string/find/
i have tried this when i enter 1 it shows consonant i dont know why this is happening?


Can you show us the code?
Topic archived. No new replies allowed.