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
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.
#include <vector>
usingnamespace std;
void do0() { }
void do1() { }
typedefvoid(*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
}
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..
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.."?
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.
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?
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.
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.
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.