Hey, I am only leraning c++ at the moment so sorry if this sounds a stupid question or if the answer is really obvious but anyway...

How do you have a variable name itself?

What I want the program to do is to take the input from the user and create a new variable for it called say "variable1" and store it there. Then for the second input I would like the program to create another variable called "variable2" and so on all the way up to "variablen"

I have thought about using another variable say "x" and have the program create a new variable called "variable" and add x on the end, but I am not sure how to do that...

Any help would be appriciated.

Thankyou.

Recommended Answers

All 10 Replies

You cant do that in c++. All variables must be declared before compilation. if you want to get n inputs you will either need an array or some sort of container like a vector.

What you are looking for is a dynamic array container, in C++, there are many containers, the most usual being std::vector , find info on it here.

Here's a very simple example:

#include <iostream>
#include <vector>

int main() {
  std::vector<int> variables;
  int x;
  std::cout << "Enter a number (0 to quit): ";
  std::cin >> x;
  while(x != 0) {
    variables.push_back(x);
    std::cout << "Enter a number (0 to quit): ";
    std::cin >> x;
  };
  std::cout << "You entered:" << std::endl;
  for(int i = 0; i < variables.size(); ++i)
    std::cout << variables[i] << std::endl;
  return 0;
};

As I am new to c++ as I already said would you mind explaing what variables.push_back(x) does as that is the only part I didnt understand...

push_back() is a function that pushes an element into the vector

std::vector<int> variables; // this creates an empty vector of int variables
variables.push_back(1); // is the same thing as variables[0] = 1, except you are not allowed to set values using "variables[0] = 1"; if you do, your program will crash
variables.push_back(1000); // this will set the second element in the array to 1000( variables[1] = 1000 )
// and so on...

Thanks for the help. In point 3 though you mention "...the second element in the array..." Is it possible to have multi-dimensional vectors like it is with arrays?

>>Is it possible to have multi-dimensional vectors like it is with arrays?

Yes. The way std::vector works (and all other STL containers) is that you provide, between < > , the type of elements you want to contain in the vector. Now, in my example, std::vector<int> will contain "int" values. If you want a multi-dimensional vector, just make a vector of vectors, as in std::vector< std::vector<int> > . And you can access the elements with something like vect_2dim[i][j] .

And just a precision on dospy's statement. You are allowed to write and do variables[0] = 1; , but the first element (accessed by index 0) must already exist in the vector before you do this. Xide, I suggest that you take a bit of time looking at the documentation about std::vector, it's better than us trying to describe every useful function it contains.

Since you are "leraning c++ at the moment", I assume it's from a class. If so and you've never heard of vectors, don't bother with them. Your instructor will get there eventually. Your best bet is the array idea.

And just a precision on dospy's statement. You are allowed to write and do variables[0] = 1; , but the first element (accessed by index 0) must already exist in the vector before you do this.

that's what i meant by saying you cannot initialize a vector this way, did not mention that you can't modify or access them after initialization.
sry for being ambiguous tho :D

Your instructor will get there eventually.

If he's lucky. More likely is that his instructor will go through the gamut of lower level and error prone C-style solutions, and only mention the "STL" in passing.

If he's lucky. More likely is that his instructor will go through the gamut of lower level and error prone C-style solutions, and only mention the "STL" in passing.

Haha. When I took my first C++ class, we dealt with arrays extensively, 2-3 weeks. And towards, the end he introduces vectors for like 20 minutes of the last class. But I have to say, I learned a lot more this way.

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.