How do I return the value of an integer with the same name as a string

This is a discussion on How do I return the value of an integer with the same name as a string within the C++ Programming forums, part of the General Programming Boards category; Is there a way to input the name of a string and return the value of an integer that has ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    49

    How do I return the value of an integer with the same name as a string

    Is there a way to input the name of a string and return the value of an integer that has the same name as the string? For example, suppose I have this program...

    Code:
    #include <iostream>
    using namespace std;
    
    int main(){
        string nameofstring = "test";
        int test = 1;
        
        cout << nameofstring;  //outputs the word "test", I would like it to output the integer 1.  How do I do this?
    
        cin.get();
        return 0;
    }
    I would like it to read the name of the string and find the return the integer of the same name as the string. I think it can be done in Java using getInteger( String nm, int val ). Is this possible in C++? Thanks.

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    You can't do exactly what you're looking for in c++. It's known as reflection in other languages and is absent from C++.

    However, you can get pretty close by using a std::map
    Code:
    #include <iostream>
    #include <map>
    
    using namespace std;
    
    int main(){
        std::map<string, int> namedNumMap;
        namedNumMap["test"] = 1;
        
        cout <<  namedNumMap["test"] ;  //outputs 1.  
    
        cin.get();
        return 0;
    }
    it's not perfect, but it's as close as you're gonna get in C++
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    49
    Thx ChaosEngine, I'll give it a try.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Don't forget to #include <string> if you are using the string class (even if it appears to work without it).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM

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