-3

I'm trying to create a loop that will will create a new variable but also change the name of the variable, such as increasing in value, automatically. Not sure if this is possible because you cant have dynamic variables?

if (cin.get() == '\n')
{
    m ++; // Add an integer to m
    string (1 + m); //Trying to name the string the value of m + 1, i.e 3
    cin.ignore():
    getline(cin, (1 + m))
    myfile << (1 + m) << endl;
}

That is my current code which is full of errors but hopefully readable enough to gain an understanding of what I'm trying to do.

Bad title, don't know what else to call it.

Matt
  • 21
  • 1
  • 5
  • You cannot change variable's name, you can create a new one. – Maroun Feb 21 '16 at 07:46
  • What I'm trying to do is the create a new variable with the name that is changing. – Matt Feb 21 '16 at 07:47
  • @Matt *Trying to name the string the value of m + 1, i.e 3* Number are not allowed for variable name. Can you describe why are you trying to do this ? – tchelidze Feb 21 '16 at 07:47
  • 3
    I think the first thing to do is open the book on C++ and start reading – Ed Heal Feb 21 '16 at 07:48
  • @tchelidze Trying to name it like that so that every time the if statement is run it would create a new variable with a different name. – Matt Feb 21 '16 at 07:51

3 Answers3

2

You're misunderstanding C++ if you try to "rename a variable".

C++ is a compiled language in which the names of the variables only exist in your source code as a means of handling them, but not in the compiled program.

You hence can't also create a variable with a specific name at runtime -- variable names simply don't exist then.

You probably want some kind of container that maps values to keys, think of a dictionary, where you can say "for this integer value 12, I store the string monkey" or so. Have a look at std::map.

Now, you're really not making much sense right now; and try to do things that aren't really the way C++ works for anyone who learned it in a ordered manner. I'd really recommend getting a C++ book or tutorial and start with that. It's going to turn out to safe you a lot of time! Here's a list of recommended C++ books and ressources.

Community
  • 1
  • 1
Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • @Marcus_Müller What i'm trying to do is create a new variable each time the if statement is run but with a different name each time. In the example I was trying to use an already existing int variable while adding a value to it and trying to use that value as the name of the string. – Matt Feb 21 '16 at 07:54
  • @Matt "trying to do is create a new variable each time the if statement is run ..different name" It **cannot** work. really. Try to understand my answer. – Marcus Müller Feb 21 '16 at 07:56
  • Thanks for the help, I guess I'll start from the beginning again. – Matt Feb 21 '16 at 08:02
1

You can't. It's not possible in C++.

To declare and initialize a variable in each iteration is however possible:

for (int i = 0; i != 10; ++i) {
        int var = i; // Declare 'var' and assign value of 'i' to it.  
}                    // 'var' object goes out of scope and is destroyed

Names are visible from the point where they are declared until the end of the scope in which the declaration appears. Names have scope and Objects have lifetimes.

Andreas DM
  • 10,685
  • 6
  • 35
  • 62
0

The closest thing to "renaming" in C++ would be declaring a reference of the "old named" variable with the new name.

int a = 2;
int &b = a;
//you can now call either `b` or `a` to get the value

Creating new variables on the fly is not possible. The correct insert any compiled language name here way of doing it is by pushing values into a container (array, map, stack..etc). With maps, you can do something similar to what you want, but it's different one only "maps" a string to a value.

#include <map>
#include <string>
#include <sstream> //for int to string conversion

std::string stringify(int val)
{
  std::stringstream ss;
  ss << n;
  return ss.str();
}

int main()
{
  std::map<std::string, int> values;
  for (int i = 0; i < 10; i ++){
    values[stringify(i)] = i*100;
  }
  std::cout << values["1"]; // prints 100
  std::cout << values["9"]; // prints 900
  return 0;
}
Aiman Al-Eryani
  • 709
  • 4
  • 19