user1745511
user1745511

Reputation: 67

Simulating and shuffling a deck of cards for C++?

I am new to C++ and I need to simulate a deck of cards and be able to randomly pull out a card from the deck. But I am having problems with my coding:

  #include <iostream>
#include <cstdlib> //for rand and srand
#include <cstdio>
using namespace std;

string suit[] = {"Diamonds", "Hearts", "Spades", "Clubs"};
string facevalue[] = {"Two", "Three", "Four","Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "King", "Ace"};

string getcard()
{string card;
int cardvalue = rand()%12;
int cardsuit = rand()%4;
card += facevalue[cardvalue];
card += "of";
card += suit[cardsuit];
return card;
}

int main ()
{int numberofcards = 0;
for (int = 0; i < numberofcards; i++)
{cout << "You drew a" << getcard() << endl;}

}

When I try to compile it says:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data

I'm not sure what I am doing wrong.

Also I don't know how to make my program so it only draws a single card once rather than infinitely.

Any suggestions?

Upvotes: 1

Views: 6096

Answers (4)

Paul
Paul

Reputation: 11

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
enum Suit{clubs,diamonds,hearts,spades};
const int jack=11;
const int queen=12;
const int king=13;
const int ace=14;
////////////////////////////////////////////////////////////////////////////////
class card
{
private:
int number;
Suit suit;
public:
card()
{}
void set(int n,Suit s)
{suit = s; number =n;}
void display();
};
//------------------------------------------------------------------------------
void card::display()

{
if(number>=2 && number<=10)
cout<<number;
else
switch(number)
{case jack: cout<<"J";break;
case queen: cout<<"Q";break;
case king: cout<<"K";break;
case ace: cout<<"A";break;
}
switch(suit)
{
case clubs:cout<< static_cast<char>(5);break;
case diamonds:cout<< static_cast<char>(4);break;
case hearts:cout<< static_cast<char>(3);break;
case spades:cout<< static_cast<char>(6);break;
}
}
////////////////////////////////////////////////////////////////////////////////
int main()  
{card deck[52];
int j;
cout<<endl;
for (j=0;j<52;j++)
{
int num=(j%13)+2;
Suit su=Suit(j/13);
deck[j].set(num,su);
}
cout<<"\nOrdered Deck: \n";
for(j=0;j<52;j++)
{
deck[j].display();
cout<<" ";
if(!((j+1)%13))
cout<<endl;
}
srand(time(NULL)); 
for(j=0;j<52;j++)
{
int k=rand()%52;
card temp= deck[j];
deck[j]=deck[k];
deck[k]=temp;
}
cout<<"\nShuffled Deck: \n";
for(j=0;j<52;j++)
{
deck[j].display();
cout<<",";
if(!((j+1)%13))
cout<<endl;
}
getch();
return 0;
}

this program will help you shuffle the cards do the rest on your own.

Upvotes: 1

Abu_of_the_Night
Abu_of_the_Night

Reputation: 34

As the other answers have said, you need to include the "string" library by adding "#include " to the top of your file. You forgot to declare "i" in your for loop as well. Your facevalue array is missing "Queen" and once you add "Queen", you also need to change how you generate cardvalue so that it is rand()%13. Also, in order to make sure that you do not deal the same card twice, you need to keep track of which cards have been dealt and then include a check in getcard() that will draw a new card of the current card has already been drawn (you may need to draw multiple times to get an undrawn card).

Here is a quick and simple way to do the check (I have modified and corrected your code to show it but remember that this is not the best way to do it):

#include <iostream>
#include <string>
#include <cstdlib> //for rand and srand
#include <cstdio>
using namespace std;

string suit[] = {"Diamonds", "Hearts", "Spades", "Clubs"};
string facevalue[] = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};
int numberOfCardsDrawn = 0;
string drawnCards[52];

string drawCard () {
    string card;
    int cardvalue = rand()%13;
    int cardsuit = rand()%4;
    card += facevalue[cardvalue];
    card += " of ";
    card += suit[cardsuit];
    return card;
}

bool isMyCardAlreadyDrawn (string card) {
    for(int i = 0; i < 52; i++) {
        if(card.compare(drawnCards[i]) == 0) { // if this is true, then both strings are the same
            return true;
        }
    }
    return false; // if the code reaches this point, then the card has not been drawn yet
}

string getCard () {
    string card = drawCard();
    while (isMyCardAlreadyDrawn(card) == true) { // keep drawing until an undrawn card is found
        card = drawCard(); // draw a new card
    }
    drawnCards[numberOfCardsDrawn] = card;
    numberOfCardsDrawn++;
    return card;
}

int main () {
    int numberOfCards = 52;
    for (int i = 0; i < numberOfCards; i++){
        cout << "You drew a " << getCard() << endl;
    }
}

Upvotes: 2

Syntactic Fructose
Syntactic Fructose

Reputation: 20076

string is not a built in type for c++, you need to manually include the library in order to use the data type string. Your compiler does not recognize the keyword string because you never included the library, so it thinks string is the variable name. The library would be included with:

#include <string>

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258578

You forgot to

#include <string>

Upvotes: 3

Related Questions