473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to avoid a lot of switch case

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?
Jun 27 '08 #1
2 1594
On Thu, May 29, 2008 at 11:26:34PM -0700, si**********@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";
....
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
Jun 27 '08 #2
On May 30, 9:52 am, Szabolcs Borsanyi <s.borsa...@sussex.ac.ukwrote:
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]);
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
8
by: Markus Dehmann | last post by:
I defined a base class in order to put heterogeneous values into a standard container: All values that I store in the container are derived from my base class. Now when I iterate over the...
10
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the ...
10
by: Evie | last post by:
I understand that when a switch statement is used without breaks, the code continues executing even after a matching case is found. Why, though, are subsequent cases not evaluated? I wrote a...
11
by: ME | last post by:
In C# the following code generates a compiler error ("A constant value is expected"): public void Test(string value) { switch (value) { case SimpleEnum.One.ToString(): MessageBox.Show("Test...
13
by: Fei Liu | last post by:
Hi Group, I've got a problem I couldn't find a good solution. I am working with scientific data files in netCDF format. One of the properties of netCDF data is that the actual type of data is only...
2
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
14
ADezii
by: ADezii | last post by:
The tendency of VBA code to evaluate all expressions, whether or not they need to be evaluated from a logical standpoint, makes the use of the IIf(), Switch(), and Choose() Functions inefficient and...
18
by: desktop | last post by:
I have 3 types of objects: bob1, bob2 and bob3. Each object is identified by a unique ID which gets returned by the function getId(). All bobs are descendants from class BaseBob which is an...
3
by: digz | last post by:
This is a very simplified version of something I am trying to understand. The State object holds the strings and maps , and I pass a reference to State to the process Function which manipulates it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.