Thread: Function Pauses

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    68

    Function Pauses

    I have a function which shuffles a deck of cards the problem is when the function is called again it sits there and does nothing

    Code:
    void shuffleDeck(int wDeck[4][13])
    {
    	
    	int card, row, column;
    	
    	/* Randomize */
    	srand(time(NULL));
    	
    	for (card=1;card<=52;card++)
    	{
    		row = rand() % 4;
    		column = rand() % 13;
    		
    		while (wDeck[row][column] != 0)
    		{
    			row = rand() % 4;
    			column = rand() % 13;
    		}
    		
    		wDeck[row][column] = card;
    	}
    	
    }
    I have no idea why it does that. This pauses even when its in a smaller app just to debug the function so its not the I am using in my program that uses it.

  2. #2
    Quote Originally Posted by gsoft
    I have a function which shuffles a deck of cards the problem is when the function is called again it sits there and does nothing
    Code:
    void shuffleDeck(int wDeck[4][13])
    {
    	int card, row, column;
    	
    	/* Randomize */
    	srand(time(NULL));
    srand() must called once and only once at the very begining of your code (in main(), probably).
    Emmanuel Delahaye

    "C is a sharp tool"

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    68
    Ok I took it out and put it in the main function however I dont think srand() is the problem, as its still pausing.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    68
    Ok I did some debugging with checking where the program was stopping. I found that the while () loop was the cause as it was finding 0 as it wasnt defined to 0 I made another function which goes through the deck and makes them 0

    Code:
    void deleteDeck(int wDeck[4][13])
    {
    	
    	int row, column;
    	
    	for (row=0;row<4;row++)
    		for (column=0;column<13;column++)
    			wDeck[row][column] = 0;
    	
    }

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You could replace that whole deleteDeck function with:
    Code:
    memset(wDeck, 0, sizeof(int) * 4 * 13);
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    68
    Thanks totally forgot about memset

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM