Answer the question
In order to leave comments, you need to log in
How to declare variables in a loop in c++?
The task before me is a simple task: to create several variables similar to each other with different names:
Counter1
Counter2
...
CounterN
How I imagine it:
in the for loop, declare different variables, indicating the value i in curly braces:
for (int i(1); i <= n; i++)
{
int Counter{i} = i;
}
Answer the question
In order to leave comments, you need to log in
In C++, you cannot create variables at run time. You can do something like this:
std::vector<int> Counter;
for (int i = 0; i <= n; i++)
{
Counter.push_back(i);
}
std::map<int, int> Counter;
for (int i = 0; i <= n; i++)
{
Counter[i] = i;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question