473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 16644
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

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

Similar topics

6
by: DigitalRick | last post by:
I have been running CDONTS in my ASPpages to send emails to me sent from my guestbook. It had been working fine untill I upgraded to Server 2003 (I am also running Exchange 2003) all locally. I...
7
by: RCS | last post by:
Okay, a rather 'interesting' situation has arisen at a place I work: I need to convert a database from Access to something that can be used over the web. I am currently maintaining and...
9
by: Coleen | last post by:
Hi All :-) I found the way to get my column sum (Thanks Cor I did it a little different, but the result is what I wanted) I used: dt_stat_report_3b.Columns.Add(New DataColumn("Sum",...
9
by: Paul M. Cook | last post by:
OK guys, this is my first real attempt at VB. Actually my return to writing programs since 1998. I've got an old DOS QB app that I am converting to VB 6.0. Really simple file formatting app. ...
1
by: John_H | last post by:
Re: ASP.NET 2.0 I would like suggestions or code examples on how to collect a variable length list of input data (item# & item quantity specifically). I thought that I could accomplish this...
5
by: Testguy | last post by:
Hi, I was wondering if one the smart people that frequent this group could give me a hand with a small program I am attempting to debug. I am not a highly experienced developer, but can...
9
by: anupamjain | last post by:
Hi, After 2 weeks of search/hit-and-trial I finally thought to revert to the group to find solution to my problem.(something I should have done much earlier) This is the deal : On a JSP...
3
by: Howler | last post by:
Hello all, I am having a hard time seeing what I am doing wrong with a program I am having to write that converts pbm monochrome images into a similar pgm file. The problem I am having is...
2
by: Killer42 | last post by:
The Input #1 statement simply reads in one line from a text file (in this case you INI file) and places the values from it into one or more variables. So what you are reading in this statement is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.