Based on the code you provided I recommend that you use a vector
because it can be filled with an arbitrary amount of elements. vector
is a generic class which will handle arbitrary user defined types as elements (read here to get understanding of generics). The variable has to be declare as member variable. If you declare it inside a method, the variable is only accessible in there and not in other methods. Here is an example how to use the vector in your context:
#include <vector>
class main_menu {
private:
std::vector<long> random_digits;
public:
void random_number();
void search_array();
};
In the implementation you fill the vector like this:
using namespace std;
void main_menu :: random_number() {
int Size = // init it with 10 or 20;
for(int index = 0; index < Size; index++) {
long random = (rand()% Size) + 1;
// this line fills the vector
random_digits.push_back(random);
cout << random << ' ';
}
cout << endl;
}
In the other method you can access your elements like using an array:
void main_menu :: search_array() {
cout << "First element: " << random_digits[0] << endl;
}
Look also at the documentation of vector.