GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Oct-2003, 13:28
Lejmer Lejmer is offline
New Member
 
Join Date: Oct 2003
Posts: 3
Lejmer is an unknown quantity at this point
Question

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  
Old 07-Oct-2003, 11:41
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
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  
Old 07-Oct-2003, 12:31
Lejmer Lejmer is offline
New Member
 
Join Date: Oct 2003
Posts: 3
Lejmer is an unknown quantity at this point
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  
Old 08-Oct-2003, 07:06
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
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 GIDBlogA Week in Kuwait by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:29.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.