Private variables lose value between functions

This is a discussion on Private variables lose value between functions within the C++ Programming forums, part of the General Programming Boards category; Code: class grid { private: unsigned xSize, ySize, gridValues; string gridFile; int generateRandom(int); public: grid() { xSize = 1; ySize ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    8

    Private variables lose value between functions

    Code:
    class grid
    {
    private:
    	unsigned	        xSize,
    				ySize,
    				gridValues;
    	string		gridFile;
    
    	int generateRandom(int);
    
    public:
    	grid()
    	{   
    		xSize = 1;
    		ySize = 1;
    	}
    	grid(int sizeX, int sizeY)
    	{
    		xSize = sizeX;
    		ySize = sizeY;
    		cout << xSize << " " << ySize; Displays random numbers
    	}
    	
    	void createRandomGrid();
    	void loadGrid(string);
    	void displayGrid();
    	void displayMenu();
    };
    Code:
    int grid::generateRandom(int num)
    	{ cout << "in generateRandom" << endl;
    		return 1 + rand() % num; }
    
    void grid::displayGrid()
    	{
    		cout << "this function will return the grid as a string" << endl;
    		cout << "X:" << xSize(Displays 1) << "Y:" << ySize(Displays 1) << endl;
    		cout << "The area of your grid is " << xSize*ySize << endl;
    
    	}
    void grid::createRandomGrid()
    	{	
    		unsigned seed;
    		seed = time(0);
    		cout << "in createRandomGrid" << grid::generateRandom(seed) << endl;This works as intended
    		grid::grid(grid::generateRandom(seed),grid::generateRandom(seed));
    	}
    So what happens is in grid::createRandomGrid the random number comes out ok, and generateRandom is working ok. My xSize and ySize values are being set by the Grid(int sizeX, int sizeY) but as soon as it finishes my createRandomGrid function and moves into displayGrid function the values of xSize and ySize are set to 1.

    What am I not doing to keep these values set?

    sorry if its confusing

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    8
    ok just realized its running my Default Constructor after my Overloaded. anyone know why?

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Show us how you are intializing your grid object. Not the ctor but how you are calling it. If you just call it like:
    grid myGrid;

    Then the first ctor is being called and they are set to 1.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    we still need to see how you are calling it.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    5,871
    In createRandomGrid, you're calling a constructor. That creates a new object, which is immediately destroyed. It does not re-invoke the constructor for the current object. Instead of
    Code:
    grid::grid(grid::generateRandom(seed),grid::generateRandom(seed));
    do
    Code:
    xSize = grid::generateRandom(seed);
    ySize = grid::generateRandom(seed);
    Unrelated to your problem, but the resolution of time() is pretty low: there is no guarantee it will produce different values on consecutive calls of createRandomGrid(), if those calls are very close in time.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    8
    @grumpy
    The project specifically states to use time(0) for our random number seed, otherwise I agree that it's not very "random". Other than using a user defined or predefined number for seeding, what would you recommend?

    @jeffcobb
    You are correct, the way I was calling it was the problem. I had it down as grid mygrid; and also accidently had it included in my do loop for my menu. I pullede it out of the loop, still calling it as I was, but am now changing the xSize and ySize through a function. (as per this assignment, that is NOT very clear on how it wants things...)
    I have it changed now and that fixed the problem.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,976
    Quote Originally Posted by SkylerZio
    The project specifically states to use time(0) for our random number seed, otherwise I agree that it's not very "random". Other than using a user defined or predefined number for seeding, what would you recommend?
    You do not seem to actually be using that as a seed though. What you are currently doing is using the "seed" to determine the upper limit of the range of numbers generated. This is different from what is meant by a seed for a pseudo-random number generator.

    In fact, you probably should be using this seed value with srand just once, before all those calls to grid::generateRandom(). This could very well be at the start of the global main function. Then you call grid::generateRandom() with your desired upper limit, rather than with seed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. about private class variables
    By bulletbutter in forum C++ Programming
    Replies: 5
    Last Post: 04-18-2008, 12:13 PM
  2. Question about Static variables and functions.
    By RealityFusion in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2005, 02:31 PM
  3. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  4. Public vs. Private variables in classes
    By blankstare77 in forum C++ Programming
    Replies: 13
    Last Post: 08-31-2005, 05:43 PM
  5. Static variables in functions
    By PJYelton in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2002, 01:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21