Thread: store function in string array? possible?

  1. #1
    Registered User
    Join Date
    May 2005

    store function in string array? possible?

    Im quite a noobie in c++ so hope i dont need to be ashamed now
    Its like this.. when i use my menu function I want to have a string array as input and in that string I want to have my function stroed.
    So when I let user choose menu choice with nb then I just use the the string and function start...?
    Possible? couse I tried store function in string but I donīt get it work...

  2. #2
    Registered User
    Join Date
    Apr 2003
    In C++, you can only store the designated types in an array. So, if you have an array that is type string, you can't store functions in it. The type of a function includes the type of its return value and the type of its parameters.

    You could employ what are called "parallel" arrays: one array for the strings and one array for the functions. The function corresponding to stringArray[0] would be stored at funcArray[0], and the function corresponding to stringArray[1] would be stored at funcArray[1], but all the functions would have to be the same type, i.e they would all have to have the same return type and parameter types.

    If you are just beginning, the easiest way to do what you want is to use a series of if-else if statements that compare the input to the elements of the string array. If the input matches one of the elements in the string array, then call the function you want.
    Last edited by 7stud; 05-03-2005 at 09:30 AM.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Welcome sappy,

    Yes, I think if-satements are what you are looking for. In general, this is called conditional branching, and it's how computers "make decisions". In C++, conditional branching is done with If-statments and switch-case statements.

    Conditional branching and looping are the two most important programming concepts (in any language).

    Storing the function in a string will not work because your computer can't "run" C++. Normally, the C++ code is compiled to machine code, which the computer can run, but the function-string would remain un-compiled ASCII text.

    Suggestions for user-friendly keyboard menus:
    - Allow upper or lower case input
    - Allow a single character to be input (i.e. 'Y' = YES)
    - Create a default input... (i.e. ENTER is the same as 'Y')
    - Allow for invalid input... loop-back, or treat invalid input as default.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM