![]() |
|
#1
|
|||
|
|||
Just help !!Gah didn't come up with a good topic.
Anyways, I need help creating two functions, first off, I need help to create: new_cmd("command name", "function"); I want it to firstly add a command name to an array or something, so the program remembers it, and add a function that it also will remember, but not use (in this function). So if I enter: new_cmd("quit", "cmd_quit"); it remembers quit and cmd_quit and associate quit with cmd_quit; so when I enter this on the code: new_cmd("quit", "cmd_quit"); printf("\n]"); scanf("the_input"); check_cmd("the_input"); it creates the command "quit", then inputs for a command, then checks for a valid command in the variable the_input, and calls the function specified in the new_cmd call. If I entered quit in the prompt it calls the function cmd_quit. Did you get it right? Well, I need another thing to be done... I need help with the function check_cmd too. It has to work with the new_cmd function, otherwise, it would be unnessesary to even define it, wouldn't it? Any help will be appreciated ;P |
|
#2
|
|||
|
|||
|
OK, the first question anyway. Yes you can make an association between the 2 function names using a simple enough linked list of 2 strings per link.
So new_cmd() will add 1 link to the list, containing "quit" and "cmd_quit" check_cmd() will then scan along the links looking for "quit", and return it's alias "cmd_quit". Have I lost you? Then problem I'm seeing is how to get the program to execute the (say) cmd_quit() function on the fly at runtime. Either a big switch/if statement, or pointers to functions, not handy if you're calling C functions! Do reply if you want me to go slower GF |
|
#3
|
|||
|
|||
|
Hehe, yes you could go a little slower
or maybe show me ... I'm "kinda" new to c++ so I am not fimiliar to all these words used in c++ Please, show an example, or at least explain the words Thanks! |
|
#4
|
|||
|
|||
|
OK, here's the deal.
To do what you are asking, you've to store pairs of (alias & command) strings together. Best you should create a simple object Pairs that holds 2 strings, the command & it's alias. You want to be able to add to this list while the program is running - either create an array which is as large as you'll ever need, or use something called a linked list. A linked list is an array which will grow as large as you like, but it's a little harder to use. The C++ STD (Standard Library) calls it a vector. You can keep a linked list/vector of objects - so it can handle your Pairs. Then the check_cmd() function should scan across the list, checking every 'link' (think of it as a chain) for the alias, and if found, returns the real command. If you've not heard of linked lists yet, best look up a good boook on them. It'll explain them better than I can. GF |
Recent GIDBlog
A Week in Kuwait by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The