"variable generator"

Is there any way for a new variable (not a value for a variable) to be made each time a loop is run. Like

cin >> t1
cin >> t2
cin >> t3
.
.
.
cin t1000

but then be able to use the combination of these values? Like to have a calculator where all of these values are added together? Thanks in advance
This can be achieved using a function
1
2
3
4
5
6
7
8
9
10
11
12
int gen() //function
{
 int x;  //variable declared
 return x; //return that variable
}
int main()
{
 int array[20],i;
 for(i=0;i<20;i++)
 array[i]=gen(); //using for loop variables are generated and stored in an array
 return 0;       //however if you are going to store values in an array you don't need all those variables
}

Can also be done without function
1
2
3
4
5
6
7
8
9
10
int main()
{
  int i,array[20];
  int sum=0;
  for(i=0;i<20;i++)
  cin>>array[i];
  for(i=0;i<20;i++)
  sum+=array[i];
  return 0;
}
Last edited on
Yes. Study std::vector
You can also use dynamically alocated arrays. realloc() would be helpful here.
Topic archived. No new replies allowed.