How can i create, set, and retrieve a user defined variable when i have a name they've given me?
How can i create, set, and retrieve a user defined variable when i have a name they've given me?
Try looking into the STL map object.
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
I assume your talking about they enter bob and 5 and your program creates a variable called bob and sets it equal to 5. This isnt possible in normal C/C++. This is only common in SCRIPTED languages (I wrote a scripted language for one of my programs, and incorporated this feature, i love perl btw) The easiest way to do this in C++ would be to create a STL map of user created variables.
So as you can see, you cant create bob, but you can use standard template library map objects to store data in key,value pairs and basically do the same thing as u want. If your interested in doing stuff like this with functions later on (user defined functions at run time =P ) just drop me a line.Code:#include <map> ...bla bla bla... std::map<string,int> userdata; ...read in user data name as string, value as int... userdata[string dataname] = valueint; ...later on... std::cout << dataname << " is: " << userdata[dataname] <<std::endl; ...you can also access something directly like this... std::cout << "bob is: " << userdata["bob"] <<std::endl; ...
edit: RATS! Beaten again!
it's yelling at me saying that it has a problem with the arguments.
it says
the code isCode:Main.cpp(142) : error C2974: 'map' : invalid template argument for '_K', type expected c:\program files\microsoft visual studio\vc98\include\map(140) : see declaration of 'map' C:\Program Files\CICK-IDE\FlagVal2\MMFireSDK\V2Template32\Main.cpp(142) : error C2974: 'map' : invalid template argument for '_Ty', type expected c:\program files\microsoft visual studio\vc98\include\map(140) : see declaration of 'map'
Code:short WINAPI DLLExport DeclareValue(LPRDATA rdPtr, long param1, long param2) { char * p1=(LPSTR)param1; std::map<p1,0>; return 0; }
You were trying to use variables as template types...if you haven't already read up on templates...Code:#include <map> #include <string> using namespace std; int main() { map<string,int> mymap; string name; int value; cout<<"Enter a name:\t"; cin>>name; cout<<endl<<"Enter a value:\t"; cin>>value; mymap[name]=value; cout<<mymap[name]; cin>>value; }
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
That's why it is not a good idea for a newbie to do everything with the STL...
I suppose you won't listen to me but you should really do it by yourself instead of trying to use something you don't know about.
If you don't see how, at least, you need those things:
- a structure with a key member and a value member, of the types you want them to be
- a list (if you choose a linked-list implementation) or an array (if you choose an array implementation) containing structures you created; it has to be sorted
- a pointer to either the beginning of your list or your array
- an insert and extract functions (procedural implementation) or methods (object-oriented one); they aren't that necessary if you just use a linear search but a hash is faster
At the time I learnt, I did everything by hand, just having both the pleasure to understand my code and see it working.
The STL is the only reasonable way to do what he wants in C++. There was no other choice than introduce him to templates. He doesnt have to know how they work, or why they work, as long as he can reproduce them when needed.Originally posted by lyx
That's why it is not a good idea for a newbie to do everything with the STL...
Reasonable, eh? It's a matter of opinion. Anyway, I think that for learning purpose, he'd better do with what he knows. But that's my opinion, everyone is free to do as he thinks, however, I can tell by experience that my method works well. How can one become good at programming if he always rely on the easy way?The STL is the only reasonable way to do what he wants in C++. There was no other choice than introduce him to templates. He doesnt have to know how they work, or why they work, as long as he can reproduce them when needed.
i thought of my own method to use. and it isn't too slow
there is only one problem with your other suggestions, there is technically no user interface but a popup menu.