Thread: Determining function based an input

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76

    Determining function based an input

    I have a menu, where the user enters a command followed by a argument, ex:
    add 5 // adds 5
    sub 7 // sub
    in x 8 // inserts x into the 8 th spot

    Right now I am tokenizing the string, looking at the first token to determine what function to use and using the following tokens as arguments to pass.

    Is there a better way to do this?

    PS. In PHP I did the same using regular expressions.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i think maybe in java i saw something that could execute functions based on a string, but i think you need to use a big if/else structure to determine the appropriate function to use.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76
    I am using if/else know to check the first token, but how can can I compare a string that looks like: add 15 without breaking it up.
    Code:
    cin >> x;
    if (x == "add..... ??????)

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    assuming x is a string, i imagine using substr or find_first_of functions would be suitable. ie:
    Code:
    if ( x.find_first_of("add", 0) == 0)
       cout << "found function 'add num'' << endl;
      else if ( x.find_first_of("...", 0 == 0 )
      // ...
    i would think would work fine--but is untested.

    edit:

    once you find out what function the token corresponds to, to see if it has valid input, you can try and convert the rest of the string (from position token.length() + 1 to x.length() ) to a number (integer/double, whatever your accepting. for example, with input x = "add 15", we have x.length() = 6, token = "add", token.length() = 3 so the number we are expecting to be in indices 4 and 5 (x.length() + 1 - token.length() = 6 + 1 - 3 = 4 to the end of the string). the +1 is to handle the delimeter between function and parameters being a space. if you dont require or are unsure of how many spaces the user will be entering, i think stringtokenizer would be best.

    hope it helps
    Last edited by nadroj; 02-19-2008 at 08:19 PM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76
    Cool that works, thanks.
    I have another idea, let me test it and I will get back.

    PS.
    Code:
      string x;
      cin >> x;
      if ( x.find_first_of("add", 0) == 0)
          cout << "found function 'add num'" << endl;
      else if ( x.find_first_of("sub", 0) == 0)
          cout << "found function 'sub num'" << endl;
      else if ( x.find_first_of("in", 0) == 0)
          cout << "found function 'in num'" << endl;  
    ...

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I don't think find_first_of does what you think: it finds the occurence of any character given in the argument. x.find_first_of("add", 0) will return 0 if x is "da", "div", "asc" etc.

    If x contains anything more than the command (+whitespace + numbers), you might try
    - starts_with from boost string algorithms (or write it yourself)
    - use string::find and see if that returns 0
    - compare a substring up to first whitespace
    etc.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably want to have a list of strings with corresponding functions (e.g. functor objects or function pointers), so that you don't have to build a long list of if/else branches. If you keep the list sorted or use a hashed list of some sort, you can also improve the speed with which you can find the right string - but a linear search is fine to begin with, just think about the fact that storage of the symbols may change for improved performance in the future.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76
    Quote Originally Posted by anon View Post
    I don't think find_first_of does what you think: it finds the occurence of any character given in the argument. x.find_first_of("add", 0) will return 0 if x is "da", "div", "asc" etc.

    If x contains anything more than the command (+whitespace + numbers), you might try
    - starts_with from boost string algorithms (or write it yourself)
    - use string::find and see if that returns 0
    - compare a substring up to first whitespace
    etc.
    Yes, I noticed that. Couple of commands had simmilar names, thus typing one in called the wrong function.
    But since I only have few commands, I changed their names and everything is smooth.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determining if input is int, double, or char
    By Lucid003 in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2005, 04:16 PM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. How do I allow a user to input a function?
    By bananasinpajama in forum C++ Programming
    Replies: 9
    Last Post: 05-19-2005, 11:11 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM