Large switch case in C

Asked

Viewed 592 times

4

I am developing a system for the college and I would like to know what is the best option, in the sense of optimization of the code, of improving even.

I have a switch Menu case, where I have 88 cases.

I have a screen called "Help", on this screen, the user can choose between 88 screens.

Example below:

To access the "Help" screen, he must type "help"

You will access this screen telaAjuda (); with several options within it. Such options are between 1 and 88. Example below:

printf("\n\t Op1 ...................................1\n");
printf("\n\t Op2....................................2\n");
printf("\n\t Op3....................................3\n");
printf("\n\t Op4....................................4\n");
printf("\n\t Op5...................................5\n\n");
...
printf("\n\t Op88...................................88\n\n");

And according to what is chosen, the user is taken to another screen of determined number (as if it were a book paging scheme).

If he chooses and type the number 3, he will be taken to screen Op3.

The screen I use the switch case is the subMenu_Ajuda () ,who calls her under the help(I made two separate functions for this, because if I want to call the help submenu elsewhere in the code, I won’t necessarily need to call the help screen together), just to receive the value typed by the user and from it, choose the option on switch case.

Example below subMenu_help:

printf("\nnomePrograma : loginUsuario -> "); //usarlogin
scanf("%i", &opAjuda);

    switch (opAjuda) { //88 Cases

    case 1:
    system("clear");
    telaAjuda1 ();
    __fpurge(stdin);
    break;

    case 2:
    system("clear");
    telaAjuda2 ();
    __fpurge(stdin);
    break; 

    ...

    case 88:
    system("clear");
    telaAjuda88 ();
    __fpurge(stdin);
    break; 
}

I can leave the switch case same size or better I create a vector for all cases and call you when necessary?

  • 2

    It is possible, would have to create a vector with pointers to function. It is a more advanced feature that you should not have learned yet. You want me to put an answer?

  • Yeah, we’re on file-saving now. We’re not there yet. If you can show me the way, I’d appreciate it. Just where to look and what to look for helps me a lot.

  • What the fpurge does?

  • 1

    __fpurge(stdin); is used on linux, is the same thing as fflush(stdin); windows ;)

1 answer

3


I will not go into other points, I will only do what is asked.

Creates a array with pointer for functions. All functions need to exist and be accessible by the code to get their addresses.

To access the function, first we take the corresponding address according to the option typed. The syntax for calling a function by its address is to find the pointer followed by passing the parameters. In case there are no parameters, just put the ().

Remember that all functions need to be compatible. Have the same signing (same return and same parameters).

printf("\nnomePrograma : loginUsuario -> "); //usarlogin
scanf("%i", &opAjuda);
//seria bom validar se está entre 1 e 88
void (*opcoes[])() = {&telaAjuda1, &telaAjuda2, ..., &telaAjuda88}; //assinatura
system("clear");
(*opcoes[opAjuda - 1])();
__fpurge(stdin);

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Obviously there in ellipsis need to put the name of all functions using the operator & taking your address from each function, which obviously needs to exist.

  • 1

    Oh cool. I’ll try to do here. Thank you. ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.