|
-
January 24th, 2010, 11:47 AM
#7
Re: What am i doing wrong??
 Originally Posted by VladimirF
Well, this is what I said; it’s one way to do it. Not the best.
Are those x, y and z the same in both of your structures? You could extract that common data into its own struct, than knows how to randomize its data members:
Code:
int randgen(int upper)
{
return rand() / (RAND_MAX / upper + 1);
}
struct CommonData
{
int x;
int y;
int z;
void randomize(int upper)
{
x = randgen(upper);
y = randgen(upper);
z = randgen(upper);
}
};
Then you could have that struct as a part of your other classes:
Code:
struct Field
{
CommonData cd;
};
struct House
{
CommonData cd;
};
Then your main() might look like that:
Code:
int main()
{
Field ball;
House house;
srand((unsigned)time( NULL ));
ball.cd.randomize(10);
house.cd.randomize(10);
}
that is brilliant, thank you very much ^^
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|