Run time function call problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nadeem Afroz
    New Member
    • Jan 2008
    • 11

    Run time function call problem

    Hello Folks!!!!!

    Here is a question ...

    I have two functions

    void fun1(int i )
    {
    printf("%d",i);
    }

    void fun2(float f)
    {
    printf("%f",f);
    }

    I want to call the fun1 if the user-input is 1 and fun2 if the user-input is 2
    I need to do this without using if-else, switch or conditonal operators... Any way to accomplish this ???
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Try an array of pointers to functions.

    Comment

    • Nadeem Afroz
      New Member
      • Jan 2008
      • 11

      #3
      Originally posted by Banfa
      Try an array of pointers to functions.
      Array of pointers to functions doesn't work here because the functions are of different types (arguments are of different types).... Please suggest any other way...

      Comment

      • newb16
        Contributor
        • Jul 2008
        • 687

        #4
        Code:
        (input-2)||(printfloat(2.5),1);
        (input-1)||(printint(3),1);

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          You want your program to do one of two things based on an input value. This is by definition conditional code. You can't avoid conditional operators; the best you can do is hide them as suggested by the array index and || suggestions posted earlier.

          What topics were being discussed in your class around the time this assignment was given out?

          Has your teacher provided a specific definition of what the "conditiona l operators" are? Perhaps there's a loophole if that definition isn't complete.

          What argument is passed to these functions? That is, what number do you expect to see printed by them? Perhaps you can define wrapper functions for each of them such that the wrappers have identical prototypes; then you could use function pointers.

          Cheers,
          donbock

          Comment

          Working...