MISTAKE! MISPLACED ELSE!***

Following is a program about "To check whether a character is a Vowel or a consonant".

#include <iostream.h>
#include <conio.h>
#include <ctype.h>
void main()
{

char ch;
cout<<"Enter a character: ";
cin>>ch;

if(isalpha(ch))
cout<<"It is an alphabet";
{

switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':

cout<<"It is a vowel";break;
default:cout<<"it is a consonant";

}
}
else
cout<<"Not an alphabet";
getch();

}

The ERROR is in the third last statement : In the last else statement it says misplaced else...

PLEASE HELP!!!!!!!
You need to put the entire clause for the if statement in {}'s. Right now, the logic says if (isalpha(ch)) cout << "...";
Because there are no {}'s, that is all that is controlled by the if statement.

If you want everything from the cout to the end of the switch, you need to move the "{" after the cout to before the cout.

Note: please use code tags. It makes it much easier to read and comment on your code. Click the button that looks like "<>" and paste your code between the generated tags.
Thank you So much DOUG!!!
Have a great day!
Topic archived. No new replies allowed.