Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to avoid a lot of switch case

Reply
Thread Tools

How to avoid a lot of switch case

 
 
silusilusilu@gmail.com
Guest
Posts: n/a
 
      05-30-2008
I wrote ,as homework, a program that displays words after key pressed.
So, if letter 'A' is pressed, the word 'dog' appears; if letter 'B' is
pressed 'cat' appears; if letter 'a' is pressed 'apple' appears; and
so on...
I wrote this program with a lot of switch case, so i want to obtain a
smaller program (if possible)...can you help me?
 
Reply With Quote
 
 
 
 
Szabolcs Borsanyi
Guest
Posts: n/a
 
      05-30-2008
On Thu, May 29, 2008 at 11:26:34PM -0700, wrote:
> I wrote ,as homework, a program that displays words after key pressed.
> So, if letter 'A' is pressed, the word 'dog' appears; if letter 'B' is
> pressed 'cat' appears; if letter 'a' is pressed 'apple' appears; and
> so on...
> I wrote this program with a lot of switch case, so i want to obtain a
> smaller program (if possible)...can you help me?


What you would like to do is to map integers to strings.

How about an array?
const char *my_lovely_words[UCHAR_MAX];

Please stop reading here, try to write the program, and then you may continue.

my_lovely_words['a']="apple";
....
But I do not see how that would save you much keystrokes.

In C99 you can have named initialisers
const char *my_lovely_words[]={
['A']="dog",
['a']="apple",
};

Szabolcs


 
Reply With Quote
 
 
 
 
vippstar@gmail.com
Guest
Posts: n/a
 
      05-30-2008
On May 30, 9:52 am, Szabolcs Borsanyi <s.borsa...@sussex.ac.uk> wrote:
> On Thu, May 29, 2008 at 11:26:34PM -0700, silusilus...@gmail.com wrote:
> > I wrote ,as homework, a program that displays words after key pressed.
> > So, if letter 'A' is pressed, the word 'dog' appears; if letter 'B' is
> > pressed 'cat' appears; if letter 'a' is pressed 'apple' appears; and
> > so on...
> > I wrote this program with a lot of switch case, so i want to obtain a
> > smaller program (if possible)...can you help me?

>
> What you would like to do is to map integers to strings.
>
> How about an array?
> const char *my_lovely_words[UCHAR_MAX];
>
> Please stop reading here, try to write the program, and then you may continue.
>
> my_lovely_words['a']="apple";

And what if 'a' has the value 4325? It would waste a lot of space.
Here's another solution:

const char *my_lovely_words[] = { ... };
const char *p, str[] = "#Az";
int c;
....
if(p = strchr(str, c)) != NULL) printf("%c = %s\n", c,
my_lovely_words[p - str]);
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
First time in one site,lot of free fun,quality DVDmovies,songs,games,drivers and lot of entertainment. nehatoor290@gmail.com C Programming 0 02-22-2009 05:57 PM
avoid lot of try/catch blocks evgueni.titov@gmail.com Java 6 02-09-2008 05:49 AM
How to avoid runtime switch/case code based on data type? Fei Liu C++ 13 03-06-2006 06:48 PM
Avoid wasting time or how to avoid initialization Alexander Malkis C++ 8 04-13-2004 11:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57