Converting an Input String to a Variable Name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rohdej
    New Member
    • Mar 2008
    • 4

    Converting an Input String to a Variable Name

    Hello -

    I have been all over the web and found a few posts that are somewhat related to what I'm trying to do, but none that provided me a concise answer.

    I want to prompt the user to input the name of a structure in my program. I want to then be able to manipulate that structure in my program.

    For example:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    struct myStruct {
       int x;
       int y;
    };
    
    int main() {
       string structname;
       int user_x, user_y;
       myStruct struct1;
    
       cout << "Please enter the name of the structure " 
            << "you would like to manipulate: ";
       cin >> structname; 
       cout << "Enter the x value you would like to assign the structure: ";
       cin << user_x;
       cout << "Enter the y value you would like to assing the structure: ";
       cin >> user_y;
    
       //So, if the user types "struct1" (without the quotes), I would like
       //the program to assign values to struct1.x and struct1.y as follows:
    
       structname.x = user_x; //This is where I run into trouble...
       structname.y = user_y; //I know the code is not correct, but this is just to 
                              //illustrate what I'm trying to do.
       
       cout << "X has been assigned the value of " << structname.x << endl;
       cout << "Y has been assigned the value of " << structname.y << endl;
       
       //And then theoretically, these statements would have the same effect,
       //now that struct1 has been assigned values:
       cout << "X has been assigned the value of " << struct1.x << endl;
       cout << "Y has been assigned the value of " << struct1.y << endl;
    
       cin.get();
       cin.get(); //Keeps window open until user presses "Enter" key
       return 0;
    }
    So, obviously, my dilemma here is that I need the string "structname " to refer to "struct1". I have used ".c_str()" to write to a filename of the user's input string in a program, and my hope is that there is something similiar to .c_str() to convert the string to a variable name. It doesn't look as though this is possible, from the lack of information I can find on the web so far. I have read several items about using maps, but I am unsure how to use them or whether they are the solution in this case. Any and all suggestions are apprectiated!
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    To my knowledge, you can't do that the way you want to. Use a string name entry in the struct and have the user enter that and locate the matching struct, creating one with that name if there isn't one.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      This is what Microsoft did using COM. You get to query an object for an interface (struct) by name and if the object supports that struct, you get a pointer to it.

      Comment

      • mschenkelberg
        New Member
        • Jun 2007
        • 44

        #4
        The way you would use a map is as follows:

        #include<map>
        ...

        int main()
        {
        char key[100];
        struct structA A;
        map<char *, void * > theMap;
        /* Insert all structs into the map that you have like so: */
        theMap["structA"] = (void *)&A;

        cin >> key;
        void * aStruct = theMap[key];
        if ( aStruct != NULL )
        {
        /* struct exists! */
        use if/else if statements to determine the struct if multiple exist
        and cast to that struct
        }

        }

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by mschenkelberg
          use if/else if statements to determine the struct if multiple exist
          and cast to that struct
          You are writing C. C++ does not use typecasts unless a) you are calling a relic C function , or b) your C++ design is screwed up.

          If you are intending to create a registry,as you would using singleton objects, you do store the object in the registry by pointer but you do not typecast it.

          You might read this article: http://bytes.com/forum/thread656124.html.

          Comment

          • rohdej
            New Member
            • Mar 2008
            • 4

            #6
            Originally posted by Laharl
            To my knowledge, you can't do that the way you want to. Use a string name entry in the struct and have the user enter that and locate the matching struct, creating one with that name if there isn't one.
            Thanks. That works just fine. I had been a little confused about pointers to start with, and going through this process actually helped me master pointer operations. I'm using 24 different possible instances of the same class for comparison, so I had to assign each one a string to hold the name (as well as pass each name to the constructor), then use "if-elseif-else" statements to compare with user input. So, a little tedious, but it's still well-organized and does exactly what I want.

            Comment

            Working...