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
- #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;
- }