delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/06/09/16:58:56

From: zargon AT hotmail DOT vom (Zargon)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: switch
Organization: Zargon and Zed Take Over The Universe
Message-ID: <394154a9.41962339@news.globalserve.net>
References: <8hrgn0$e19$1 AT nnrp1 DOT deja DOT com>
X-Newsreader: Forte Free Agent 1.11/32.235
Lines: 32
Date: Fri, 09 Jun 2000 20:37:26 GMT
NNTP-Posting-Host: 207.176.153.132
X-Complaints-To: news AT primus DOT ca
X-Trace: news1.tor.primus.ca 960582993 207.176.153.132 (Fri, 09 Jun 2000 16:36:33 EDT)
NNTP-Posting-Date: Fri, 09 Jun 2000 16:36:33 EDT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Fri, 09 Jun 2000 19:31:45 GMT, kknd_bl AT my-deja DOT com ate too many
hallucinogenic mushrooms and wrote:

>If I use switch , I can only pass numbers (int) to it. How can I pass
>words etc to switch and what should I type after 'case' when I do that?

You have to use integer types, not pointers (e.g. char *), structs,
classes, or floating point types in a switch. You need to use if
statements to get what you want:

if (strcmp(the_string, string1) == 0) {
  // String 1.
} else if (strcmp(the_string, string2) == 0) {
  // String 2.
} else if (..........
.....
} else {
  // Default case.
}

Of course with this you can't get the "fall-through" behavior, it's as
though every case is ended with a break, and the default case is
forced to be last. Any common behavior several cases must use can be
moved into a function that those cases call.

Incidentally, this question is about C/C++ programming in general and
not DJGPP-specific, and the language features involved are the same in
C and in C++, so this is a question better asked in comp.lang.c.

-- 
Any sufficiently advanced magic is indistinguishable from
an Allegro-using C++ program compiled with gcc.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019