I copied this program to count the total vowels..it works just fine but I don't
understand why '++vow_ctr;" is used at the end of the switch case instead with a default statement?! (pls help..urgent)
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
void main ()
{ clrscr();
char line[80];
int vow_ctr=0;
cout<<"Enter the line \n ";
gets(line);
for(int i=0;line[i]!='\0';i++)
{ switch(line[i])
{ case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
++vow_ctr;
}
}
cout<<"total no. of vowels \n" <<vow_ctr <<endl;
}
The ++vow_ctr at the end of the switch line is applicable too all of the above cases. Only a break; statement will exit the switch before the next case. Therefore the default case is not really applicable here.
Here is your code with code tags, correct indents, and formatting. This will help you to read the switch properly.
The vow_ctr variable is incremented whenever it finds a vowel. There is no need for a default statement, as the only other scenario would be the letter being a consonant, a number or a special character, all of which will be ignored, the switch statement will skip, and then repeat for the next letter.
this maybe funny but I have no idea how to use code tags..m new to this site..
But thanks all for the help!...I absolutely understood the working of the switch case now ! :)
To add code tags you just put [code][/code] around your code.
If you select the code and press the <> button to the left it will add code tags around the selected text.