Thread: switchcase is for integers, something for strings?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    70

    Post switchcase is for integers, something for strings?

    Hello, i've been reading about the switch case function and the book im reading says that switch case is only for evaluating integers and that made me think because i tried making a program using switchcase and putting case "Name": but it didnt work so i was wondering is there a function that does that?

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    No, you are going to have to find a workaround such as matching the strings to a set of int values either with a macro, an array or perhaps even some sort of enum.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well there are a few ways, but all involve you doing some work.

    The more obvious way is something like
    Code:
    enum { FILE, EDIT, VIEW };
    struct {
    	char	*name;
    	int	value;
    } table[] = {
    	{ "File", FILE },
    	( "Edit", EDIT },
    	{ "View", VIEW },
    };
    int search ( char *word ) {
    	// search table for matching name, returns value
    }
    
    switch ( search(word) ) {
    	case FILE:
    		break;
    	case EDIT:
    		break;
    	case VIEW:
    		break;
    }

    The less obvious way is to create a simple 'hash' of the keywords
    Code:
    #define TOK(x)	((x[0]<<24)|(x[1]<<16)|(x[2]<<8)|(x[3]))
    switch ( TOK(word) ) {
    	case TOK("FILE"):
    		break;
    	case TOK("EDIT"):
    		break;
    	case TOK("VIEW"):
    		break;
    }
    Simple, because it must produce a compile-time constant when used in the cases.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Was this too much?
    It was everything claudiu listed as choices....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    #define TOK(x)	((x[0]<<24)|(x[1]<<16)|(x[2]<<8)|(x[3]))
    switch ( TOK(word) ) {
    	case TOK("FILE"):    // error: case label does not reduce to an integer constant
    		break;
    I don't think simple hash way will work since switch-case needs constant expression.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. enum switchcase and windows message q
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 11-27-2006, 01:16 PM