I've begun to do a new program, but I've hit a roadblock and need help. I'm trying to enter the size of a set that has a random number generator in it. So when I enter in the size should be 3 I should get 3 random numbers, but that doesn't happen. The code will look a bit off since I wanted to see if the function actually worked before progressing further.
#include <iostream>
#include <set>
#include <ctime>
#include <cstdlib>
usingnamespace std;
class Set {
public:
//default constructor
Set();
//add element to set
void addElement(int element);
//remove element from set
void removeElement(int element);
//check for membership
bool isMember(int element);
//set union, modifies curremtn set
void Union(Set s);
//set difference modifiers current set
void difference(Set s);
//size of set
int size();
//get element i
int getElement(int i);
private:
//binary search for element, returns index
bool search(int element, int& index);
//set members
//int elements[maxElements];
//next empty position in elements
int next;
};
Set::Set()
{
}
int Set::size()
{
srand(time(0));
set<int> s{ rand() % 25 + 1 };
int S_size;
cout << "Enter in the size of the set" << endl;
cin >> S_size;
for (int i = 0;i < S_size;i++)
{
cout << s.size();
}
return s.size();
}
int main(set<int>&)
{
Set enter;
enter.size();
}
set s is destroyed after size() is done.
it seems like you should have that as a class member, not a local?
it also seems like the loop should add a random number to s every iteration, and the initial set should be empty instead of having 1 random value.
also, consider <random> instead of C's random tools.