I want integers 1-52 to be drawn only once, so every "card" will be seen and not one picked twice, heres the code

#include<iostream>
#include<time.h>
#include<string>
#include<stdlib.h>
using namespace std;
class Player{
      int hand;
      
      };
int main()
{
    srand(time(NULL));
    int deck=53;
    int *drawn;
    int notdraw[52];
    for (int n=1;n<53;n++)
    {
        deck-=1;
        drawn = new int;
        *drawn = rand() % 52+1;
        for (int i = 0; i <n; i++)
        {
            while(*drawn==notdraw[i])
            {
                                 delete drawn;
                                 drawn = new int;
                                 *drawn = rand() % 52+1;
                                 }
                                 }
            
        cout << deck << ". " << *drawn << endl;
        notdraw[n]= *drawn;
        delete drawn;
        }
        cout << deck << endl;
        system("PAUSE");
        }

Ignore the class, Ill be messing with it later

Recommended Answers

All 7 Replies

The rand() function does not return a pointer to an int. It just returns the int. (reference)

So attempting to delete it is causing your error.


Also, while % does precede + in precedence, the way you have it written looks scary to me. Personally, I always tend to use () whether or not they are needed, just to be explicit. (I have often tried to debug another's code where he didn't use parentheses in a complicated expression and I have to wonder if he really meant it to do what it does.) drawn = (rand() % 52) + 1; That'll get you your card number in 1..52. Be careful: the notdraw[] array takes subscripts of 0..51. I would just make it a 53 element array and ignore the 0th element.

Watch your indentation too. Looks like a good start. Personally, I would also make the deck of cards a class. :) (But there you certainly don't have to.)

Hope this helps.

The rand() function does not return a pointer to an int. It just returns the int. (reference)

So attempting to delete it is causing your error.

Wait, are you talking about this line:

*drawn = rand() % 52+1;

rand() returns an int but drawn has been dereferenced so isn't it also of type int?

Hmm, right you are. (I misread it as a newbie mistake, which he didn't actually make...)

I guess what I was trying to say is that there is no need to use a pointer.

The pointer is never initialized to point to valid memory, so trying to dereference it is like firing random bullets toward the propane shed. Better just to use the integer directly than incur the overhead of allocating and dereferencing a pointer.

Within the program, I would always stick to the 0 to 51 notation. If you start using 1 to 52, you are most propably going to be confused sooner or later.
The code is most propably not going to work correctly, because you use notdrawn before initializing it.
Also, I would suggest declaring the variables before seeding the random number generator.
I think, you are looking for something like this:

#include<cstdlib>
#include<ctime>
#include<iostream>
#include<string>
using namespace std;
int main()
{
    int deck=52;
    int nextdraw=0;
    bool notdraw[52]={false};
    srand(time(0));
    for (int n=0;n<52;n++)
    {
        deck--;
        do
            nextdraw=rand()%52;
        while(notdraw[nextdraw]);
        notdraw[nextdraw]=true;
        cout << "card to draw: " << nextdraw << " cards left: " << deck << endl;
    }
    system("PAUSE");
}

But i may have misunderstood you. One thing is for certain though: You do not need to declare drawn as a pointer.

yeah I was just messing around with pointers, ill get rid of em and report back to you guys sometime later, its 7 am here and I'm tired as hell... Night

There is also propably a much more elegant way, in which the checks for an already drawn card are unnecessary. For instance, if you implemented a deck class, which holds a list of cards and is able to randomly shuffle them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.