cases

This is a discussion on cases within the C++ Programming forums, part of the General Programming Boards category; Ok so I just read up on cases. I made a program that is supposed to say if something is ...

  1. #1
    ___
    Join Date
    Jun 2003
    Posts
    806

    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?
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    161
    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.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to handle multiple cases which use common code?
    By tmaxx in forum C Programming
    Replies: 3
    Last Post: 10-03-2008, 08:42 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. I need to use 0 to quit a program
    By cwest in forum C Programming
    Replies: 2
    Last Post: 12-15-2001, 08:37 AM
  5. Please help me with cases!!!
    By halifax in forum C Programming
    Replies: 1
    Last Post: 10-14-2001, 02:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21