Thread: going to a another function

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    going to a another function

    what im trying to do is go to another function, that the user enters..

    Code:
    #include <stdio.h>
    char choice[4];
    
    int main() {
     printf ("enter a choice: ") ;
     scanf ("%s", choice);
     printf ("your choice is %s", choice);
    
     }
    after " printf ("your choice is %s", choice);"
    i want it to go to a function named after the variable choice...
    for example if they enter math, i want it to go to the "math" function.
    how would i do this , without a IF statement?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > how would i do this , without a IF statement?
    With huge amounts of difficulty.
    'C' isn't that kind of language.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Create a structure that contains a name and a function pointer, where all functions share the same prototype. Use an array of said structures, fixed at compile time. Use a loop + strcmp to check said array for selected name, and call the chosen function.

    [edit]Of course, that usually would contain at least one if. I just noticed you didn't want any. Heh. That'll be more of the "huge amounts of difficulty". Or if not that, "exerciese in pointlessness".[/edit]

    Now if you don't want them to all share the same prototype, that's where the "huge amounts of difficulty" come in...


    Quzah.
    Last edited by quzah; 08-20-2005 at 12:49 PM.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167

    2 cents

    My 2 cents.
    Make a 2d array funcname storing the name(or the choices).Take a variable int a.
    make a for loop.like for(i=0;strcmp(choice,funcname[i]);i++);
    then you have value in int a corresponding to which function you want to call.Use a switch statement there.

  5. #5
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    what happened nobody responded.nobody pointed out an error.is everyone gawking at my stupidity

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You mean the strcmp() == 0?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    you could also get around the strcmp. Since choice is only 4 char's long, you can use a switch statement.

    Code:
    switch( *((int *)choice) )  //this makes C think all 4 of those char's 
     {                            //are individual bytes of an in
    
        case 'eno': //C will see this as an integer in the same fashion as well
                 DoThisFunction();
                 break;
     
       case 'wto':
                 DoThisOtherFunction();
                 break;
    }
    I have those words backwards because I'm assuming you're working with a little endian machine. If you're big endian, write them as you would in proper English. If you don't know what endian means, you should find out.

  8. #8
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    from what you say i gather it's the same with Intel/Motorola
    Two strings walk into a bar. The first one says, 'Bartender! Bartender! I want a drink!'. The second one says, 'Bartender! Bartender! I want a drink too! Blaaaaaaaaah eeeeeeeek yaaaaaaak oooooooh'. The first one says, 'Please excuse my friend. He isn't null term--'.

  9. #9
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Quote Originally Posted by dwks
    You mean the strcmp() == 0?
    yup.strcmp returns int 0,if both strings are same.is'nt it? and in c it is considered as false so loop will break

  10. #10
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Quote Originally Posted by skorman00
    you could also get around the strcmp
    Code:
    switch( *((int *)choice) )  //this makes C think all 4 of those char's 
     {                            //are individual bytes of an in
    
        case 'eno': //C will see this as an integer in the same fashion as well
                 DoThisFunction();
                 break;
     
       case 'wto':
                 DoThisOtherFunction();
                 break;
    }
    Dont you think it will create some problem if the choices are of length 1 or 2?.May be i am wrong

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well if you're allowing switch/case, you can hide

    > how would i do this , without a IF statement?

    as

    Code:
    switch ( (cond) != 0 ) {
      case 1:  /* stuff for then*/
        break;
      case 0:  /* stuff for else*/
        break;
    }
    > switch( *((int *)choice)
    Not only do you have endian problems, you have alignment problems.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Quote Originally Posted by Salem
    > switch( *((int *)choice)
    Not only do you have endian problems, you have alignment problems.
    If you're talking about having choices that are < 4 characters, then yes you have to be a bit tricky. If not, would you mind elaborating?

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    From the gcc manual
    -Wcast-align
    Warn whenever a pointer is cast such that the required alignment of the target is increased. For example, warn if a char * is cast to an int * on machines where integers can only be accessed at two- or four-byte boundaries.
    On some machines, accessing mis-aligned data will get you a 'bus error' exception.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM