CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    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);
    	
    }

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: What am i doing wrong??

    Quote Originally Posted by Mariusmssj View Post
    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...

  3. #3
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: What am i doing wrong??

    Quote Originally Posted by VladimirF View Post
    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)
    	}

  4. #4
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: What am i doing wrong??

    Quote Originally Posted by Mariusmssj View Post
    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.
    Code:
    *(pBall+i)
    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...

  5. #5
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: What am i doing wrong??

    Quote Originally Posted by VladimirF View Post
    WHAT error?
    You’d get more help if you specify the error you get, instead of making me guess.
    Code:
    *(pBall+i)
    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

  6. #6
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: What am i doing wrong??

    Quote Originally Posted by Mariusmssj View Post
    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...

  7. #7
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: What am i doing wrong??

    Quote Originally Posted by VladimirF View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured