LOOOOOOOOOOOOONG switch statement

Sep 26, 2010 at 6:13pm
So, I have a program I decided to make purely for practice purposes, since I'm learning C++, but I came across a problem: my switch statements are becoming HUGE, which is ugly, and time consuming. I was wondering if the any alternative to

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main(int *argv, char *argc[]) {
 int X = 0;
 std::cout << "enter a number between 0 and 99:" << std::endl;
 std::cin >> X;
 for (int i = 0; i < X; ++i) {
  ++X;
 }
 switch(X) {
  case 0: {
   //do something
  }
  case 1: {
   //do something else
  }
...
  case 99: {
   //LONG SWITCH STATEMENT
  }
  default: {
   std::cout << "the number you entered was invalid" << std::endl;
 }
 return 0;
}


for example. This switch statement would take hours to write and take up hundreds of lines, it just seems like terrible way to do something.
Last edited on Sep 26, 2010 at 6:14pm
Sep 26, 2010 at 6:16pm
1. That for loop will never end if the user enters a number higher than 0.
2.
1
2
3
4
5
6
7
8
if(X >= 0 && X <= 100)
{
	std::cout << "Valid Number";
}
else
{
	std::cout << "INVALID NUMBER!";
}
Last edited on Sep 26, 2010 at 6:17pm
Sep 26, 2010 at 6:20pm
Yes, you can create an array of function pointers aka a jump table:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <vector>

using namespace std;

void do0() { }
void do1() { }

typedef void(*ptr2Func)(void);

static vector<ptr2Func> myJumpTable;

void init() 
{
  myJumpTable.push_back( do0 );
  myJumpTable.push_back( do1 );
}

void invoke( int x )
{
  *(myJumpTable[x])();  // I leave it up to you to validate x values
}

Sep 26, 2010 at 6:23pm
Nevermind
Last edited on Sep 26, 2010 at 6:29pm
Sep 26, 2010 at 6:27pm
Whats wrong with that?
OP implies that each case does something different ("//do something" and "//do something else"). He probably didn't mean it, but if that's the case, kfmfe04 is right. Also, if that's the case, then there are probably some design flaws..
Sep 26, 2010 at 6:45pm
thanks for the replies!

1. L B, thats not the issue at all ...it's an example, it's not perfect. I typed it off the top of my head real quick. But thanks for pointing it out though :)

2. kfmfe04, thats really complicated, I do use vectors in my program but that's over my head, however, I guess I'll implement it and learn how it works that way. Thanks for the reply.

3. hamsterman, to make myself a little clearer: they each do something different of course (thus the "//do something else"). But what do you mean "there are probably some design flaws.."?
Sep 26, 2010 at 6:46pm
If you need to manually do 100 different things depending on user input then your program has a design flaw because you should never be in that situation.
Last edited on Sep 26, 2010 at 6:46pm
Sep 26, 2010 at 6:55pm
hm I didn't think about it that way ...I guess that's true, but its just a question :) thanks everyone kfmfe04 solved it so I'm marking it as solved.
Sep 26, 2010 at 6:59pm
If you need to manually do 100 different things depending on user input then your program has a design flaw because you should never be in that situation.

Not really. What if you are writing an Eliza program?

http://en.wikipedia.org/wiki/ELIZA

or a Zork game?

http://en.wikipedia.org/wiki/Zork

In those cases, you would want something like:
 
static map<string,ptr2Func> myJumpTable;  // a REAL jump table 

In fact, a C++ compiler is doing a 100 things based on user input (source files).

liyarax, don't worry if it's over your head for now. Eventually, you'll learn about pointers, function pointers, and stl.

FYI, jump tables is just a technique that compilers use.
Last edited on Sep 26, 2010 at 7:01pm
Sep 26, 2010 at 7:00pm
http://www.cplusplus.com/doc/tutorial/pointers/ All the way down it talks about function pointers.
Sep 26, 2010 at 7:24pm
I'm not saying that it is always a bad way to do things. I have a feeling this may be the case though. I'd like OP to say what exactly he's trying to do. Maybe I'm wrong.
Sep 26, 2010 at 7:29pm
hamsterman has a point: for example, sometimes virtual methods could be a valid (OOP way) of eliminating giant case statements.

Anyways, I think the OP was just playing around with ideas and trying things out, but if he were trying to do something specific, we could probably come up with the most appropriate approach.
Sep 26, 2010 at 7:41pm
mhm, I don't actually have switch in my program right now, I just got thinking "what if I needed to do THIS?" so I decided to post this to answer my question. by the way, I got most of that jump table down thanks to google, except I still really dont get what
1
2
3
void invoke(int x) {
 *(JumpTable[x])();
}

does at all, in fact I get a compile error: void value not ignored as it ought to be.
Last edited on Sep 26, 2010 at 7:42pm
Sep 26, 2010 at 7:47pm
You may need to put the * inside the parenthesis.

It looks up the address at JumpTable[x] and dereferences the pointer, thereby invoking the function.
Sep 26, 2010 at 7:57pm
oh ok, i see. Thanks, this got the results I wanted; thanks for the help. :)
Topic archived. No new replies allowed.