2

Hello guys i´m new to programming and i ran into a problem. I have a String and I use the lenght of the String to create variables according to the lenght of the string. I want to use a string("usagetxt_whl_str") as the name of my integer. Every time the loop runs it should create a new variable with a diffrent name. for example: 1_var, 2_var and so on

QString usagetxt = "example"
int usagetxt_len = usagetxt.length();
int usagetxt_whl = 0;
QString usagetxt_whl_str = QString::number(usagetxt_whl);
while (usagetxt_whl != usagetxt_len){
usagetxt_whl = usagetxt_whl + 1;
**here im trying to create my var**int usagetxt_whl_str + "_var" = 0;* }

How can I get this to work? Is it even possible?

  • 3
    Don't do this. Just create a vector, and add the numbers to the vector. Then you can access each number using a plain number via `[]`. – Carcigenicate Sep 02 '18 at 17:14
  • 4
    If you really need the string value to distinguish and come back there later, consider a `std::map` to store these values. – πάντα ῥεῖ Sep 02 '18 at 17:16
  • 3
    I have a strange feeling of deja vu. Didn't you already ask this today? – StoryTeller - Unslander Monica Sep 02 '18 at 17:18
  • Thanks for the answers! ^^ StoryTeller yes I did reupload it because it was marked as a duplicate. But that didnt helped me at all. – oltmatthi Sep 02 '18 at 17:20
  • You want an integer variable whose name you can change just like a string? – Galik Sep 02 '18 at 17:22
  • Galik I want a while loop that creates an integer, which name should be every time diffrent. for example: 1st time = 1_var, 2nd time = 2_var ,3rd time = 3_var. I don't know if its possible. Maybe you know another way to do this! – oltmatthi Sep 02 '18 at 17:26
  • You just need to associate a string with an integer. You can use `std::pair` or `std::map` depending on what you want to do with it/them after. Can you elaborate your use case? – Galik Sep 02 '18 at 17:28
  • Basicly I look how long my string is and want to create for every character a diffrent number which should be stored in integer for every character each. – oltmatthi Sep 02 '18 at 17:29
  • Names (Official term: identifiers) exist only in the code and some debugging files. By the time the program is compiled and linked, all of the nice, human readable names have been replaced with the memory addresses and offsets from memory addresses that computers can understand. Since the names are all gone by the time the program can be run, one cannot create new names. See other comments about `std::map` for one way to "name" a variable at runtime. – user4581301 Sep 02 '18 at 17:30
  • So it sounds like you want to associate every character with an integer. Something like `std::map` then? – Galik Sep 02 '18 at 17:31
  • Thank you Galik I will try that! – oltmatthi Sep 02 '18 at 17:32

1 Answers1

3

C++ is a compiled language. So you cannot create a variable name at run-time.

However there are several alternatives that can address the need of having a dynamic number of "variables" (in reality of values). The first one is to use a vector. Here you create a vector that contains usagetxt_len integers :

std::vector<int> myvars(usagetxt_len); 

You can then access each of these integers with the traditional indexing operator:

myvars[i] = i;

As indexing is numeric (starting with 0 of course), you can easily process them in loops.

Another approach to dynamic "variables" is based on strings. You can then define a name in a string, and access the variable:

std::map<string, int> myvalues; 

You could then access specific values associated to strings:

myvalues["4_var"]=0;  
myvalues["5_var"]=myvalues["4_var"]+3; 

As you are new to programming, I think that the vectors will do the job. It's just a change in the way you think on a group of values.

Christophe
  • 68,716
  • 7
  • 72
  • 138