# Possible to create n number of variables?

 Ryann Mahajan · December 5, 2014 Hi, I would like to know if there's a way to have n number of variables/ array elements in my program? So, if the user enters any number of say,integers they get placed in num1,num2....and so on. Thanks

## Replies

 Alf Nohr · December 5, 2014 I'm not quite sure what you mean, but something like this?``#include using namespace std;const int Numbers = 2;int main() { int num[Numbers]; cout << "Enter the first number: "; cin >> num[1]; cout << endl; cout << "Enter the second number: "; cin >> num[2]; cout << endl; cout << "The first number have been plased at num[1], with a value of: " << num[1] << endl; cout << "The second number have been plased at num[2], with a value of: " << num[2] << endl; return 0;}`` Ryann Mahajan · December 6, 2014 That works, but what if we don't know beforehand how many numbers we have to enter ,that is, what if we don't have the value of int Numbers ? Alf Nohr · December 6, 2014 I guess you have to use a random numbers for that, but is this what you was thinking about?``#include #include using namespace std;int main() { srand(time(0)); int randNumbers = 1+rand()%5; int num[randNumbers] = {}; switch (randNumbers){ case 1: cout << "You have to enter 1 number: " << endl; cin >> num[1]; cout << "num[1] = " << num[1] << endl; break; case 2: cout << "You have to enter 2 numbers: " << endl; cin >> num[1] >> num[2]; cout << "num[1] = " << num[1] << endl; cout << "num[2] = " << num[2] << endl; break; case 3: cout << "You have to enter 3 numbers: " << endl; cin >> num[1] >> num[2]; cin >> num[3]; cout << "num[1] = " << num[1] << endl; cout << "num[2] = " << num[2] << endl; cout << "num[3] = " << num[3] << endl; break; case 4: cout << "You have to enter 4 numbers: " << endl; cin >> num[1] >> num[2]; cin >> num[3] >> num[4]; cout << "num[1] = " << num[1] << endl; cout << "num[2] = " << num[2] << endl; cout << "num[3] = " << num[3] << endl; cout << "num[4] = " << num[4] << endl; break; case 5: cout << "You have to enter 5 numbers: " << endl; cin >> num[1] >> num[2]; cin >> num[3] >> num[4]; cin >> num[5]; cout << "num[1] = " << num[1] << endl; cout << "num[2] = " << num[2] << endl; cout << "num[3] = " << num[3] << endl; cout << "num[4] = " << num[4] << endl; cout << "num[5] = " << num[5] << endl; break; } return 0;}`` Star Lord · December 6, 2014 ``#include using namespace std;int main(){ int num=0; //since we are not sure of the how many arr[number] user want we rather ask them or use the infinate loop an  cout<<"how many numbers do you want to enter: "; cin>>num; int arr[num]; //create an array of numbers using the for loop for (int a = 0; a < num; a++) { cout<<"Enter Element:   "<>arr[a]; } cout<<"\nlist of numbers entered: "; for (int a = 0; a < num; a++) { cout<#include using namespace std;int main(){vector vec;int i;while (cin >> i) vec.push_back(i);//Do something with vectorreturn 0;}``
• 1

## C++

118,498 followers