Thread: Advanced switch statements

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    13

    Advanced switch statements

    problem starts at an unknown value.
    the amount of options for the switch statement is dependent on the user.
    lets say that there are 3 options made by the user via his inputs.
    can i make a switch statement that adapts to the user input?

    input:
    option 1
    option 2
    option 3

    display:
    [1]option 1
    [2]option 2
    [3]option 3
    choice:

    then code-wise the switch statement goes like this.
    switch(choice)
    case (userinput1): something happens break;
    case (userinput2): something happens break;
    case (userinput3): something well, happens break;
    default: invalid input please choose again.break;

    in reality you wont know how many "case"s there will be it just depends on how many inputs the user makes.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kuletako327
    in reality you wont know how many "case"s there will be it just depends on how many inputs the user makes.
    Therefore, a switch statement is not appropriate since the number of cases will be fixed at compile time.

    Perhaps a dynamic array would suit the problem better, or some other way to map the option value with something.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    No, you can not do that with switch statements. That requires your program modifying the source code, recompiling itself and replacing the running image in memory.

    For something like this, you generally need to keep some sort of mapping between choices (looks like an int in your case) and actions (possibly via function pointers). If the user gets to make up menu choices how will the user know what action is tied to what choice?

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    13
    i see thanks for the replies.. imma try a linklist kind of options.. ill see if that would work better. working on my computer programming project. due in 8hrs ) hahaha.. on the final stages of my program just a little buggy in the mean time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch statements
    By shawnk01 in forum C Programming
    Replies: 3
    Last Post: 07-12-2011, 10:42 PM
  2. switch statements.
    By elixon in forum C Programming
    Replies: 2
    Last Post: 11-30-2006, 05:57 PM
  3. switch statements
    By joshua in forum C Programming
    Replies: 3
    Last Post: 11-21-2005, 03:26 AM
  4. Need help with if /else statements - maybe switch
    By dwinslett in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2003, 07:23 AM
  5. Switch Statements
    By blackgingr in forum C Programming
    Replies: 3
    Last Post: 10-07-2002, 02:36 PM