Function Pointer Menu in C

I'm having a lot of trouble figuring out how to correctly use function pointers. Specifically in a text menu with an option of choosing a number, and then performing the selected function. I already have the functions written, but cannot get the menu system working. Could someone please show an example of how this is done?

Comments

  • Here's an example of what I'm working with. I've only included one function because I'm trying to get one to work before adding the others.

    #include
    #include
    #include

    void func1(int);

    int main(void)
    {
    void(*f[1])(int);
    f[1] = func1(int)

    int num = 1;

    while(num > 0 && num < 5)
    {

    printf("Which Function Would You Like to Execute?
    ");
    printf("(1) Simulate the Roll of Two Dice
    ");
    printf("(2) The Fibonacci Series
    ");
    printf("(3) Reverse Digits
    ");
    printf("(4) Print Four Patterns
    ");
    printf("(5) Exit
    ");
    scanf("%d", num);

    (*f[num])(int a);
    }


    return 0;
    }
    void func1(int a)
    {
    int total = 36000; /* number of dice rolls variable */
    int die1, die2, sum, count = 0; /* variables for the dice, sum, and count */
    int dice[11]; /* number rolled array */

    srand(time(NULL)); /* randomize number generator */

    for (count = 0; count < 11; count++) /* initialize array */
    {
    dice[count] = 0;
    }

    printf("
    ");

    for (count = 0; count < total; count++) /* roll dice until total reached */
    {
    die1 = 1 + rand()%6; /* roll die1 */
    die2 = 1 + rand()%6; /* roll die2 */
    sum = die1 + die2; /* find the sum */
    dice[sum-2]++; /* Add 1 to number rolled in array */
    }

    for (count = 0; count < 11; count++) /* output loop */
    {
    if (count < 8) /* if/else to even output */
    printf(" %d was Rolled: %d
    ", count + 2, dice[count]); /* single digit output */
    else
    printf("%d was Rolled: %d
    ", count + 2, dice[count]); /* double digit output */
    }

    return 0; /* end of program */
    }

  • : Here's an example of what I'm working with. I've only included one
    : function because I'm trying to get one to work before adding the
    : others.
    : [code]
    : #include
    : #include
    : #include
    :
    : void func1(int);
    :
    : int main(void)
    : {
    : void(*f[[color=Red]6[/color]])(int);
    [color=Green]// you have to use 5 pointers, so the array size must
    // be properly set to 6, so you can call f[5] without
    // accessing outside of the array.[/color]
    : f[1] = func1[color=Red];[/color] [color=Green]// just a function name gives an address[/color]
    :
    : int num = 1;
    :
    : while(num > 0 && num < 5)
    : {
    :
    : printf("Which Function Would You Like to Execute?
    ");
    : printf("(1) Simulate the Roll of Two Dice
    ");
    : printf("(2) The Fibonacci Series
    ");
    : printf("(3) Reverse Digits
    ");
    : printf("(4) Print Four Patterns
    ");
    : printf("(5) Exit
    ");
    : scanf("%d", num);
    :
    : (*f[num])([color=Red]0[/color]);
    [color=Green]// when calling a function ^^^ by pointer you need to
    // pass proper parameters to it ("int a" is no good)[/color]
    : }
    :
    :
    : return 0;
    : }
    : void func1(int a)
    : {
    : int total = 36000; /* number of dice rolls variable */
    : int die1, die2, sum, count = 0; /* variables for the dice, sum,
    : and count */
    : int dice[11]; /* number rolled array */
    :
    : srand(time(NULL)); /* randomize number generator */
    :
    : for (count = 0; count < 11; count++) /* initialize array */
    : {
    : dice[count] = 0;
    : }
    :
    : printf("
    ");
    :
    : for (count = 0; count < total; count++) /* roll dice until total
    : reached */
    : {
    : die1 = 1 + rand()%6; /* roll die1 */
    : die2 = 1 + rand()%6; /* roll die2 */
    : sum = die1 + die2; /* find the sum */
    : dice[sum-2]++; /* Add 1 to number rolled in array */
    : }
    :
    : for (count = 0; count < 11; count++) /* output loop */
    : {
    : if (count < 8) /* if/else to even output */
    : printf(" %d was Rolled: %d
    ", count + 2, dice[count]); /* single
    : digit output */
    : else
    : printf("%d was Rolled: %d
    ", count + 2, dice[count]); /* double
    : digit output */
    : }
    :
    : return 0; /* end of program */
    : }
    :
    :
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories