Back to console apps.

This is a discussion on Back to console apps. within the C++ Programming forums, part of the General Programming Boards category; I learned C++ from this forum, and i made some console apps.... then i got into OpenGL..... after doing stuff ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    100

    Back to console apps.

    I learned C++ from this forum, and i made some console apps.... then i got into OpenGL..... after doing stuff like cubemapping and raytracing in 3d, i decided to see how well i could code an old fashion text-based game ...

    I have the basic structure of the game, but its been so long since programming a console app that i dont remember some stuff.

    I want this programming to take commands, for example:

    pickup sword

    i cant think of how this can be done!

    note: on linux system


    thanks!

  2. #2
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    That's rather vague......you want the user to enter "pickup sword" and the sword is picked up? That would be rather easy: get input, run it against a table of possible values.
    Do not make direct eye contact with me.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    100
    well, i guess i was too vague...

    if i used cin to get a variable cmd... how would i proccess cmd to run a function called the first word, and that function accepting the argument (the second word).

    is this the correct way of doing this?

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    I'd just use a map of strings to function pointers. Then, just look up the first part of cmd and see if there is an entry in the map. If no, tell the user you didn't understand. If yes, call the function with the second half of the cmd as the argument. I would assume all functions would take the same arguments - a single string from the cmd and anything else you wanted to pass to it.

    I don't remember the syntax for doing this, it isn't simple syntax, but I'm sure you can find it in a book or online somewhere if nobody else here can provide an example. Also, there is another thread going on about understanding user input (e.g. knowing that "get sword" is equivalent to "pickup sword"), that might help you out.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I think i get what you mean... try something like this:
    Code:
    ...
    cin>>word;
    {
         if(!strcmpi(word,"cmd")
         {
               cin>>command>>argument;
               //search for command against a list
               //check if the argument fits the command
               //if all is good, do something that has to do with that command
               //else, tell them what's wrong (command not found, etc)
         }
         cout<<"I have no clue what you're talking about";
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User trainee's Avatar
    Join Date
    Jan 2004
    Posts
    32
    Originally posted by major_small
    I think i get what you mean... try something like this:
    Code:
    ...
    cin>>word;
    {
         if(!strcmpi(word,"cmd")
         {
               cin>>command>>argument;
               //search for command against a list
               //check if the argument fits the command
               //if all is good, do something that has to do with that command
               //else, tell them what's wrong (command not found, etc)
         }
         cout<<"I have no clue what you're talking about";
    }
    Why are you using strcmp()? The string class overloads the == operator, so just use
    Code:
    if(string=="pickup sword")
    trainee

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I just never used it that way... I like strcmp better for some reason...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    Originally posted by trainee
    Why are you using strcmp()? The string class overloads the == operator, so just use
    Code:
    if(string=="pickup sword")
    trainee
    He's obviously not using string but char buffer[]. So == is not overloaded.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    major_small's idea of using if/else if/else statements will work, but will be less efficient than putting the command strings into a map. If you only have a few commands, then it should be fine, but if the number of commands is larger or is growing as you expand the program, then a map might save you time and space.

    In fact, once you write the code that does the command lookup you won't ever have to change it, because you can add as many members to the map as you want.

    And by the way, if you really feel like having fun, see if your STL implementation has a hash_map. You don't need your command strings sorted, and so hash_map will give you constant time lookup. If your program is big and has hundreds of commands, that's hundreds of string compares that aren't being performed each time the user tries to do something.

  10. #10
    Registered User trainee's Avatar
    Join Date
    Jan 2004
    Posts
    32
    Originally posted by WaltP
    He's obviously not using string but char buffer[]. So == is not overloaded.
    Show me proof that he isn't using it. He describes this nowhere.

    trainee

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console, Terminal and Terminal Emulator
    By lehe in forum C Programming
    Replies: 4
    Last Post: 02-15-2009, 08:59 PM
  2. Output to console window...
    By alvifarooq in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2004, 08:56 PM
  3. Please help with my game
    By jjj93421 in forum Game Programming
    Replies: 10
    Last Post: 04-12-2004, 11:38 PM
  4. Linux console window questions
    By GaPe in forum Linux Programming
    Replies: 1
    Last Post: 12-28-2002, 11:18 AM
  5. My opinion on visual apps
    By valar_king in forum Windows Programming
    Replies: 15
    Last Post: 10-08-2001, 05:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21