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!
:confused: JimH

Recommended Answers

All 9 Replies

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.

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

?

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

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
:D JimH

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.

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

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

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.

:D JimH

>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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.