Your 'freq' in your Prob1Random class is never initialized. IE: it's a bad pointer. So when you try to do this:
1 2 3 4 5 6 7 8
|
if(randNum = 0)
freq[0]++;
else if(randNum = 1)
freq[1]++;
else if(randNum = 2)
freq[2]++;
else
freq[3]++;
|
You're dereferencing a bad pointer. This causes heap corruption, which is the nastiest of nasty bugs. If you're lucky, the program will crash. If you're unlucky, the program will just behave strangely and it'll be very hard to figure out why. If you're really unlucky, the program will work just fine until you make some totally unrelated change a few months down the line.
(on a side note, you're using the assignment operator = in the above, not the comparison operator == which I assume was intended)
(on another note, you never initialize 'numRand' either, so it'll probably be some garbage value and won't reflect the value you expect it to)
If you want freq to be an array of 4 (is that what you're going for?), then make it an array of 4, not a pointer:
1 2
|
//int* freq; // makes a pointer
int freq[4]; // makes an array
|
There are some other oddities here. The whole if/else chain I pasted above can be simplified by using randNum as an index, rather than doing a bunch of comparison checks:
|
freq[ randNum ]++; // much simpler
|
You also have a few errors that the compiler should be giving you:
1 2 3 4 5 6 7 8 9 10 11 12
|
int *getFreq(void) const; // if you're returning a pointer
{
//return *freq; // then don't dereference your pointer. This returns an int, not an int*
return freq; // this returns the pointer
}
//Returns the set used
char *getSet(void) const;
{
//return *set;
return set; // ditto
}
|
1 2 3
|
//this actually returns a number between 0 and 3. You'll never get 4.
// is this intentional?
int randNum = (rand()%4)+0;
|
1 2 3 4 5 6 7
|
Prob1Random(const char num,const char *numSequence); //Constructor
{
nset = num;
set = numSequence; // numSequence is a const char*, but set is a nonconst char*
// this should be giving you a compiler error. You should make 'set' a const char* as well.
// this also means you'll have to change getSet() to return a const char*.
}
|
You also don't need the (void) for functions that don't have parameters. That's a weird C thing. In C++ it's pointless.