469,612 Members | 1,639 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,612 developers. It's quick & easy.

Converting an Input String to a Variable Name

4
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:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. struct myStruct {
  6.    int x;
  7.    int y;
  8. };
  9.  
  10. int main() {
  11.    string structname;
  12.    int user_x, user_y;
  13.    myStruct struct1;
  14.  
  15.    cout << "Please enter the name of the structure " 
  16.         << "you would like to manipulate: ";
  17.    cin >> structname; 
  18.    cout << "Enter the x value you would like to assign the structure: ";
  19.    cin << user_x;
  20.    cout << "Enter the y value you would like to assing the structure: ";
  21.    cin >> user_y;
  22.  
  23.    //So, if the user types "struct1" (without the quotes), I would like
  24.    //the program to assign values to struct1.x and struct1.y as follows:
  25.  
  26.    structname.x = user_x; //This is where I run into trouble...
  27.    structname.y = user_y; //I know the code is not correct, but this is just to 
  28.                           //illustrate what I'm trying to do.
  29.  
  30.    cout << "X has been assigned the value of " << structname.x << endl;
  31.    cout << "Y has been assigned the value of " << structname.y << endl;
  32.  
  33.    //And then theoretically, these statements would have the same effect,
  34.    //now that struct1 has been assigned values:
  35.    cout << "X has been assigned the value of " << struct1.x << endl;
  36.    cout << "Y has been assigned the value of " << struct1.y << endl;
  37.  
  38.    cin.get();
  39.    cin.get(); //Keeps window open until user presses "Enter" key
  40.    return 0;
  41. }
  42.  
  43.  
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!
Mar 31 '08 #1
5 14352
Laharl
849 Expert 512MB
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.
Mar 31 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
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.
Apr 1 '08 #3
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
}

}
Apr 1 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
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.
Apr 1 '08 #5
rohdej
4
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.
Apr 2 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Testguy | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.