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:
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!
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;
}
Comment