473,554 Members | 3,119 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting an Input String to a Variable Name

4 New Member
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;
  5. struct myStruct {
  6.    int x;
  7.    int y;
  8. };
  10. int main() {
  11.    string structname;
  12.    int user_x, user_y;
  13.    myStruct struct1;
  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;
  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:
  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.
  30.    cout << "X has been assigned the value of " << structname.x << endl;
  31.    cout << "Y has been assigned the value of " << structname.y << endl;
  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;
  38.    cin.get();
  39.    cin.get(); //Keeps window open until user presses "Enter" key
  40.    return 0;
  41. }
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 16712
849 Recognized Expert Contributor
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
9,208 Recognized Expert Moderator Expert
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
44 New Member
The way you would use a map is as follows:


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
9,208 Recognized Expert Moderator Expert
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
4 New Member
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

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 will include the code I originally used. I understand I should switch from CDONTS to CDO mail but after several sttempts I am finding a very hard...
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 developing a mid-sized Access database (60 tables, 25 simultaneous users). Although I've been tossing up if I should try to convert the back end to SQL...
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", GetType(Double), "sum(Column_10_ld_act_125_gtr_fy_fy_hh_avg)")) where dt_stat_report_3b is my datatable, Sum is my column name and...
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. the INPUT statement is behaving oddly. Picture a CSV file that looks like this Bill smith,123 Main St,Apt 3
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 using a GridView that has ViewState enabled, an ObjectDataSource to process the submitted list, textboxes for getting new item data and an add button. ...
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 create small programs. I was asked to write a small application that would look in a folder, and convert all the CSV files to XLS.
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 page, I want to grab a URL and parse /change the HTML and send it to the JSP page. I take the URL from the user in a textbox (not the
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 understanding how to properly convert the bit/pixel in pbm to byte/pixel in pgm. My program is very straight forward conceptually open files, read...
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 "#Please do not delete this file". My guess is that would all be placed in the first variable, a$. The information held in your file is much more...
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...

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.