Thread: User named variables

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    7

    Question User named variables

    How can i create, set, and retrieve a user defined variable when i have a name they've given me?

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    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

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    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.

    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;
    ...
    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.

    edit: RATS! Beaten again!

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    7

    doesn't work

    it's yelling at me saying that it has a problem with the arguments.

    it says
    Code:
    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'
    the code is
    Code:
    short WINAPI DLLExport DeclareValue(LPRDATA rdPtr, long param1, long param2)
    {
    	char * p1=(LPSTR)param1;
    	std::map<p1,0>;
    	return 0;
    }

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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;
    }
    You were trying to use variables as template types...if you haven't already read up on templates...
    "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

  6. #6
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    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.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    Originally posted by lyx
    That's why it is not a good idea for a newbie to do everything with the STL...
    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.

  8. #8
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    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.
    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?

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    7

    nevermind

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-17-2008, 07:45 PM
  2. MinGW, standard library, pain.
    By Tonto in forum Tech Board
    Replies: 9
    Last Post: 08-26-2006, 10:46 AM
  3. Craps Program with Local Variables
    By tigrfire in forum C Programming
    Replies: 12
    Last Post: 11-09-2005, 09:01 AM
  4. Newbie Help: Currency Converter
    By Ashfury in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 01:21 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM