Too Many Ifs
Breeze

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 sure there is a better way...

I am not using Allegro...
And I am not very skilled in programming so could you please give a slight in depth response so I know what you are talking about?

Thanks In Advance!;D

deps

use switch?

1switch( kbchar )
2{
3 case( 'a' ):
4 {
5 do_this();
6 break;
7 }
8 case( 'b' ):
9 {
10 do:_that();
11 break;
12 }
13 default:
14 {
15 do_nada();
16 break;
17 }
18}

kazzmir

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

char answer[ 64 ];
while ( something ){
    printf("Name an attribute: ");
    sscanf("%s", answer );
    if ( strcmp( answer, "name" ) == 0 )
        printf("Enter your name");
    else if ( strcmp( anser, "age" ) == 0 )
        printf("Enter your age");
}

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

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.

1void check( char *input )
2{
3 char *bingo[ 40 ] = { "yo", "sup", "peace out", NULL };
4 int i = 0;
5 
6 while ( bingo[ i ] )
7 {
8 if ( strcmp( input, bingo[ i ] ) == 0 )
9 {
10 switch( i )
11 {
12 case 0: break;
13 case 1: break;
14 case 2: break;
15 }
16 
17 return;
18 }
19
20 i++;
21 }
22}

1void check( char *input )
2{
3 switch ( input[ 0 ] )
4 {
5 case 'y':
6 {
7 if ( strcmp( "yo", input ) } {}
8 }
9 case 's':
10 {
11 if ( strcmp( "sup", input ) } {}
12 }
13 case 'p':
14 {
15 if ( strcmp( "peace out", input ) } {}
16 }
17 }
18}

curtis warren

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:

1void doHelp(){...}
2void doStuff(){...}
3 
4typedef void (*ptr)();
5 
6std::map<std::string,ptr> table;
7 
8std::string input;
9 
10table.insert(make_pair("help",doHelp));
11table.insert(make_pair("stuff",doStuff));
12 
13cout<<"Please enter a command: ";
14cin>>input;
15ptr p=table.find(input);
16if(p!=table.end())
17{
18 p();
19}
20else
21 cout<<input<<" is not a valid command!";

Btw if there are any mistakes in there let me know-since I haven't used function pointers for a while.

Chris Katko

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.

curtis warren
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.
edit:
Also, if I had a choice to function pointers instead of if statements in a game, I would definately do it, since I would rather worry about extensibility than micro-optimisations. You could, also, use polymorphism, but that is basically just function pointers with a this variable (I wonder if the compiler would optimise the this pointer out if there were no member variables?).

Ceniza

You could get the idea from here.

gillius

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.

Trezker

Hehe, you don't want to read the input code in Devospace, that's chaos if anything.;D

Thread #314178. Printed from Allegro.cc