Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating new objects...

Status
Not open for further replies.

jerryau

Programmer
Jan 14, 2004
3
SK
Hi,

I'm new to Visual C++, and this may seem like a stupid question... I'm trying to create a game, that will generate an array with random numbers, and print a different array 6 times. I got it to generate random numbers and store them into the array, but when I try to execute it 6 times to print six different arrays, it prints them all the same. Here is the relevant code:

for (int i = 1; i <= 6; i++) {
Console::Write(S&quot;Game &quot;);
Console::Write(i);
Console::WriteLine(S&quot;:&quot;);
Console::WriteLine(S&quot;-------&quot;);

int * pwNum = new int[];
PowerGen * pw = new PowerGen();
pwNum = pw->getNum();

for (int x = 0; x < 6; x++) {
Console::Write(pwNum[x]);
Console::Write(S&quot;, &quot;);
}

Console::WriteLine();
Console::WriteLine();

delete pwNum;
delete pw;
}

As you can see, I've tried to delete the pointers so that a new array with different numbers is initialised each time, but it won't work.
Does anyone have any ideas on how to fix this?

Thanks,
Jerry ;o)
 
I don't know what PowerGen is, but I will assume it's a (pseudo)random number generator which initilizes (seeds) itself in the constructor. Your probelm here is that you recreate (a thus re-initialize) the number generator in each loop iteration. Even if it initializes itself using the computer's clock, it executes it all within a second, and the number sequence will not change.

Solution:

Put the
PowerGen * pw = new PowerGen();
line outside your for loop.

Vincent
 
Thanks Vincent... it solved the problem a little... this time it generates first three and last three games the same... it still doesn't have time to generate new numbers between the three games. Is there any way to pause it a little on every itteration?

I forgot to post all of my code... here it is:

NumGenerator.cpp (main class):

// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include &quot;stdafx.h&quot;
#include &quot;PowerGen.h&quot;

#using <mscorlib.dll>

using namespace System;

int _tmain()
{
PowerGen * pw = new PowerGen();
for (int i = 1; i <= 6; i++) {
Console::Write(S&quot;Game &quot;);
Console::Write(i);
Console::WriteLine(S&quot;:&quot;);
Console::WriteLine(S&quot;-------&quot;);

int * pwNum = new int[];
pwNum = pw->getNum();

for (int x = 0; x < 6; x++) {
Console::Write(pwNum[x]);
Console::Write(S&quot;, &quot;);
}

Console::WriteLine();
Console::WriteLine();

delete pw;
}

return 0;
}



PowerGen.h:

#using <mscorlib.dll>

__gc class PowerGen {
public:
PowerGen();
~PowerGen();
int* getNum();

private:
bool contains(int* n, int y, int size);
int* allNum;
};


PowerGen.cpp (This is where the random number generation happens):

#include &quot;stdafx.h&quot;
#include &quot;PowerGen.h&quot;

using namespace System;

PowerGen::powerGen() {
allNum = new int[6];

}

int* PowerGen::getNum() {

Random* r = new Random();
int i = 0;
int temp = 0;
while (i < 6) {
temp = r -> Next(1,45);

Console::Write(S&quot;Temp&quot;);
Console::WriteLine(temp);

if (contains(allNum, temp, i)) {
temp = r -> Next(1,45);
} else {
Console::Write(S&quot;Old: &quot;);
Console::WriteLine(allNum);
Console::WriteLine(S&quot;Writing new...&quot;);
allNum = temp;
i++;
}
}

return allNum;
}

bool PowerGen::contains(int* n, int y, int size) {

for (int i = 0; i < size; i++) {
if (n == y) {
return true;
}
}
return false;
}

PowerGen::~PowerGen() {
}



Jerry
 
If you want to make the program wait, you can use
Sleep( timeinmillisec );
though it should not be necessary.

I can't believe that your code works at all, since you
delete pw;
at each iteration of your for loop, while the object is constructed only before the loop.

Now, you did not improve the situation at all, since I now see in your complete code that you still create (and thus re-initialize) your random number generator at every &quot;game&quot;, since
Random* r = new Random();
is in the getNum() method...

make &quot;r&quot; a member variable of PowerGen, and create it in the constructor, delete it in the destructor. That should work.

Vincent
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top