Possible to create n number of variables?

+1 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

Post a Reply

Replies

Oldest  Newest  Rating
+1 Alf Nohr · December 5, 2014
I'm not quite sure what you mean, but something like this?




#include <iostream>
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;
}
+1 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 ?
+1 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 <iostream>
#include <ctime>
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;
}
0 Star Lord · December 6, 2014

#include <iostream>
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:   "<<a+1;
cin>>arr[a];
}
cout<<"\nlist of numbers entered: ";
for (int a = 0; a < num; a++)
{
cout<<arr[a]<<"\t";
}
}
0 Star Lord · December 6, 2014
i guess this should be appropriate or you either use the infinite loop and set an Exit key for it 
0 Cody Treadway · December 7, 2014
I am not sure why nobody has said to use a vector.
#include <iostream>
#include <vector>
using namespace std;

int main(){
vector <int> vec;
int i;
while (cin >> i) vec.push_back(i);

//Do something with vector
return 0;
}
  • 1

C++

107,102 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator