-
cases
Ok so I just read up on cases. I made a program that is supposed to say if something is a vowel or consonant. Its not running right for some reason. Here is the code.
Code:
#include <iostream>
using namespace std;
int main()
{
char letter = 0;
cout << endl
<< "Enter a small letter: ";
cin >> letter;
switch(letter*(letter >= 'a' && letter <= 'z'))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u': cout << endl << "You entered a vowel.";
break;
case '0': cout << endl << "that isn't a small letter!";
break;
default: cout << endl << "You entered a consonant!";
}
cout<<endl;
return 0;
}
Is there any way to make the first 5 cases run as one? And why isn't it telling caps apart from undercase?
-
Change:
Code:
case '0': cout << endl << "that isn't a small letter!";
break;
to:
Code:
case 0: cout << endl << "that isn't a small letter!";
break;
Note that in the your code, the '0' is the character 0 (not the number) while your switch statement will be looking for the number 0 if the input is not lowercase.
As for the first 5 cases running as one... they already do. By making the change I mentioned above, the program works as expected for me.
-
What exactly is it doing. That 'letter*(...)' bit is a bit odd, but I think it will work fine. 'case '0':' ought to have no quotation marks though. Since multiplying your letter by zero will give you the value 0, not the character '0' (which has a non-0 ASCII representation).
>> Is there any way to make the first 5 cases run as one?
Not that I'm aware of.
>> And why isn't it telling caps apart from undercase?
It should be.