|
|
| Too Many Ifs |
|
Breeze
Member #3,778
August 2003
|
I want my program to figure out what the user inputed and see what command it was and execute accordingly. Currently I have like 5 if statements to do this. I am not using Allegro... Thanks In Advance!;D Noobie At Programming.... |
|
deps
Member #3,858
September 2003
|
|
kazzmir
Member #1,786
December 2001
|
What do you mean "what the user inputted"? Are you reading in a string from the user? Ill take a stab in the dark here.. Ill assume your program runs like this: Name an attribute: name
Enter your name: Jon
Name an attribute: age
Enter your age: 20
and then the code you need to do this would be something like Are you doing something like that maybe? Thats pretty much the best way to do it since you cant use a switch statement on strings. BTW, are you using C or C++? Also, post some code if you want a better answer/response. |
|
DanielH
Member #934
January 2001
|
If you have an set rule of what was supposed to be inputted you could work around with a switch and if or a loop.
|
|
curtis warren
Member #3,898
September 2003
|
Function pointers? I really can't answer your question any further without you explaining what you mean in depth. Anyways, here's a way you could use function pointers:
Btw if there are any mistakes in there let me know-since I haven't used function pointers for a while. |
|
Chris Katko
Member #1,881
January 2002
|
Hmm. Regardless of using Allegro. The lowest you could get would be a single if statement-per-key scanned. Except with an array of function-pointers (as mentioned above). But that may be slower then a few ifs. An if statement is extremely fast relative to seconds. Even on legacy hardware (486s, etc). So if your worried about speed, it's not something that's really taking a chunk of your CPU time. As you mentioned your using 5 ifs. 5 ifs aren't going to do a (noticable) thing to your program. -----sig: |
|
curtis warren
Member #3,898
September 2003
|
Quote: Hmm. Regardless of using Allegro. The lowest you could get would be a single if statement-per-key scanned. Except with an array of function-pointers (as mentioned above). But that may be slower then a few ifs. An if statement is extremely fast relative to seconds. Even on legacy hardware (486s, etc). So if your worried about speed, it's not something that's really taking a chunk of your CPU time. As you mentioned your using 5 ifs. 5 ifs aren't going to do a (noticable) thing to your program.
If your worried about how fast that is you could use the associative container that comes with loki (google it) which is supposedly more efficient than std::map. Not to mention using function pointers like that is much more extensible than if statements. |
|
Ceniza
Member #2,027
March 2002
|
You could get the idea from here. |
|
gillius
Member #119
April 2000
|
You can't say anything is faster than std::map. There is no code in std::map to profile. You can only say that Loki's implementation of x is faster than Microsoft's implementation of y at providing the same functionality. Gillius |
|
Trezker
Member #1,739
December 2001
|
Hehe, you don't want to read the input code in Devospace, that's chaos if anything.;D |
|
|