Variable Variable Names

Reply

Join Date: Jan 2005
Posts: 6
Reputation: JimH is an unknown quantity at this point 
Solved Threads: 0
JimH JimH is offline Offline
Newbie Poster

Variable Variable Names

 
0
  #1
Jan 11th, 2005
I need to assign a value to a variable named in a sting variable. Like this ...

int temperature;
int pressure;
int volume;

int variable_value;
char Inbuffer[2048];

printf("%s","Variable List: temperature, pressure, volume");
printf ("%s","Input variable name from list above:");
gets(Inbuffer);
ScanError=scanf("%s\n",variable_name);
....
printf("%s","Input new value for variable:");
gets(inbuffer);
ScanError=scanf("%d", variable_value);
....

// This is what I want to do . . .

// Find a simple way to convert a string representation of a variable name
// to a variable name and assign a value to it.

// For instance, if variable_name=volume and variable_value=250;
// How do I get volume=250 using the string variable_name when
// variable_name is 'volume'?


Would appreciate any help!
JimH
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Variable Variable Names

 
0
  #2
Jan 12th, 2005
make an hashmap of pointers to the variables with the name of the variable as the key.
You can then simply look up the pointer by the name of the variable, dereference it, and you have your variable back.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Variable Variable Names

 
0
  #3
Jan 12th, 2005
Or since you only have three possabilities, could it be as simple as

if (stricmp( variable_name, "temperature" ) == 0)
<set temp>
else if (stricmp( variable_name, "pressure" ) == 0)
<set pressure>
etc

?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Variable Variable Names

 
0
  #4
Jan 12th, 2005
>I need to assign a value to a variable named in a sting variable.
Why? Most of the time this problem can be solved differently by looking at it the right way. Since this appears to be C, it takes quite a bit of work to solve it properly, and that would be a waste of time if you really want something simpler but can't see it.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 6
Reputation: JimH is an unknown quantity at this point 
Solved Threads: 0
JimH JimH is offline Offline
Newbie Poster

Re: Variable Variable Names

 
0
  #5
Jan 13th, 2005
Appreciate the suggestions and advice.

I am using C for a program having about 60 user assignable system variables. Using a hash table was my first impulse, but, would like to keep additional coding and variable space to a minimal. Is there any way to use a string variabe as a variable name? However, I'll use your best advice. I haven't ventured into C++ yet, which might make things easier.

By the way, are C console programs difficult to migrate to C++?

Thanx
JimH
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Variable Variable Names

 
0
  #6
Jan 13th, 2005
You might investigate the std::map template, where you could do something like:

map<string, string> variableMap;

and then fill it in with commands like:

variableMap["CustomerName"] = "Chainsaw";

once you understand maps and iterators, the look isn't too bad.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Variable Variable Names

 
0
  #7
Jan 13th, 2005
>would like to keep additional coding and variable space to a minimal.
To do a lookup table using string keys properly, you're looking at either a hash table or a binary search tree. Those are the simplest data structures that give you good performance. If your goal is minimal extra work then the hash table is your best bet, likewise for variable space. Memory-wise you're looking at 60 string-value pairs at the very least if you want everything in high speed memory at run-time. If you don't care about ideal performance then an array of about 60 structures won't bog you down too much, even with a naive sequential search.

>Is there any way to use a string variabe as a variable name?
No, not without a lot of framework in C. In C++ you can use existing libraries to do what you would otherwise have to code manually (assuming no third party stuff).

>By the way, are C console programs difficult to migrate to C++?
It depends on the C program and the desired level of C++-correctness. For well written ISO C programs, only a few minor changes will be needed to compile as C++ (if any are needed at all). If you want proper C++ and not just C code compiled as C++ then it takes more work.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 6
Reputation: JimH is an unknown quantity at this point 
Solved Threads: 0
JimH JimH is offline Offline
Newbie Poster

Re: Variable Variable Names

 
0
  #8
Jan 15th, 2005
Originally Posted by jwenting
make an hashmap of pointers to the variables with the name of the variable as the key.
You can then simply look up the pointer by the name of the variable, dereference it, and you have your variable back.
Thanx for suggestion. Gives me something to work on.

JimH
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 6
Reputation: JimH is an unknown quantity at this point 
Solved Threads: 0
JimH JimH is offline Offline
Newbie Poster

Re: Variable Variable Names

 
0
  #9
Jan 15th, 2005
Originally Posted by Chainsaw
You might investigate the std::map template, where you could do something like:

map<string, string> variableMap;

and then fill it in with commands like:

variableMap["CustomerName"] = "Chainsaw";

once you understand maps and iterators, the look isn't too bad.
This is good input for me. Thanx.

JimH
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 6
Reputation: JimH is an unknown quantity at this point 
Solved Threads: 0
JimH JimH is offline Offline
Newbie Poster

Re: Variable Variable Names

 
0
  #10
Jan 15th, 2005
Originally Posted by Narue
>would like to keep additional coding and variable space to a minimal.
To do a lookup table using string keys properly, you're looking at either a hash table or a binary search tree. Those are the simplest data structures that give you good performance. If your goal is minimal extra work then the hash table is your best bet, likewise for variable space. Memory-wise you're looking at 60 string-value pairs at the very least if you want everything in high speed memory at run-time. If you don't care about ideal performance then an array of about 60 structures won't bog you down too much, even with a naive sequential search.

>Is there any way to use a string variabe as a variable name?
No, not without a lot of framework in C. In C++ you can use existing libraries to do what you would otherwise have to code manually (assuming no third party stuff).

>By the way, are C console programs difficult to migrate to C++?
It depends on the C program and the desired level of C++-correctness. For well written ISO C programs, only a few minor changes will be needed to compile as C++ (if any are needed at all). If you want proper C++ and not just C code compiled as C++ then it takes more work.
A lot of good food for thought. Thanx

JimH
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC