-
January 23rd, 2010, 04:07 PM
#1
What am i doing wrong??
i want to fill both structures with random numbers, i am trying to use pointers but, hmm problems
Code:
#include <iostream>
#include<time.h>
#include<stdlib.h>
struct Field
{
int x;
int y;
int z;
};
struct House
{
int x;
int y;
int z;
};
void randgen(Field*);
int main()
{
Field ball, *pBall;
pBall = &ball;
srand((unsigned)time( NULL ));
for(int i = 0; i<3; i++)
{
*(pBall+i) = randgen(&ball)
}
}
void randgen (Field member)
{
member->x = rand()/ (RAND_MAX / 10 + 1);
}
-
January 23rd, 2010, 04:16 PM
#2
Re: What am i doing wrong??
Originally Posted by Mariusmssj
i want to fill both structures with random numbers, i am trying to use pointers but, hmm problems
hmm… What kind of problems?
If I had to guess, I’d say that you have ONE Field object (ball), but you index it as if it was an array of 3 objects. Did it crush?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
January 23rd, 2010, 04:22 PM
#3
Re: What am i doing wrong??
Originally Posted by VladimirF
hmm… What kind of problems?
If I had to guess, I’d say that you have ONE Field object (ball), but you index it as if it was an array of 3 objects. Did it crush?
this part gives me an error ? it doesn't work like pointer array does it? if i want to change from x, then to y, and then z??
Code:
for(int i = 0; i<3; i++)
{
*(pBall+i) = randgen(&ball)
}
-
January 23rd, 2010, 04:49 PM
#4
Re: What am i doing wrong??
Originally Posted by Mariusmssj
this part gives me an error ? it doesn't work like pointer array does it? if i want to change from x, then to y, and then z??
WHAT error?
You’d get more help if you specify the error you get, instead of making me guess.
doesn’t point to pBall->x, pBall->y, etc.; it points to the NEXT object in the array (which you don’t have, so it just points to memory after your Field ball).
Also, your function randgen() is void, but you are assigning its return value to something.
And inside your randgen() function you keep modifying your x member variable over and over.
I would make your randgen() to simply return an int value, and assign it to x, y, and z members in the calling code.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
January 23rd, 2010, 05:04 PM
#5
Re: What am i doing wrong??
Originally Posted by VladimirF
WHAT error?
You’d get more help if you specify the error you get, instead of making me guess.
doesn’t point to pBall->x, pBall->y, etc.; it points to the NEXT object in the array (which you don’t have, so it just points to memory after your Field ball).
Also, your function randgen() is void, but you are assigning its return value to something.
And inside your randgen() function you keep modifying your x member variable over and over.
I would make your randgen() to simply return an int value, and assign it to x, y, and z members in the calling code.
like this?
Code:
#include <iostream>
#include<time.h>
#include<stdlib.h>
struct Field
{
int x;
int y;
int z;
};
struct House
{
int x;
int y;
int z;
};
int randgen(int);
int main()
{
Field ball;
House table;
srand((unsigned)time( NULL ));
ball.x = randgen(10);
std::cout<< ball.x << std::endl;
ball.y = randgen(10);
std::cout<< ball.y << std::endl;
ball.z = randgen(10);
std::cout<< ball.z << std::endl;
table.x = randgen(10);
std::cout<< table.x << std::endl;
table.y = randgen(10);
std::cout<< table.y <<std::endl;
table.z = randgen(10);
std::cout<< table.z <<std::endl;
}
int randgen (int upper)
{
int rand_number = 0;
rand_number = rand()/ (RAND_MAX / upper + 1);
return(rand_number);
}
this does work xD
-
January 23rd, 2010, 07:37 PM
#6
Re: What am i doing wrong??
Originally Posted by Mariusmssj
like this?
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);
}
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
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
|