Thread: switch case question

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    7

    switch case question

    Got a question. Can the switch-case statement be used with characters(char) inputs?
    code:

    Code:
    switch (choice)
    	{
    	case ('A'||'a'):
    		break;
    	case ('S'||'s'):
    		break;
    	case ('M'||'m'):
    		break;
    	case ('E'||'e'):
    		break;
    	case ('N'||'n'):
    		break;
    	default:
    		printf("\aError incorrect selection!!!\n");
    	}
    choice is a user input value that is read from a function. I'm sure that the character is being returned properly from the function, I've used printfs to test it. Here is the error:

    \Group_R03.cpp(30) : error C2196: case value '1' already used
    .\Group_R03.cpp(32) : error C2196: case value '1' already used
    .\Group_R03.cpp(34) : error C2196: case value '1' already used
    .\Group_R03.cpp(36) : error C2196: case value '1' already used

    it looks as it it setting a value of one to all the cases. I'm alittle confused. thanks in advance for any feedback!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    switch (choice)
    	{
    	case 'A':
    	case 'a':
    		break;
    	case 'S':
    	case 's':
    		break;
    	default:
    		printf("\aError incorrect selection!!!\n");
    	}
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    Gracias!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-05-2009, 11:32 AM
  2. switch case statement
    By stanlvw in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 05:06 AM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Changing bkgrnd color of Child windows
    By cMADsc in forum Windows Programming
    Replies: 11
    Last Post: 09-10-2002, 11:21 PM