Hey peeps, basically I'm trying to create a program that creates a deck, shuffles it, and deals hands. I have created the deck. Now I am trying to create an array to store the shuffled card, but I get Error: Invalid conversion from 'Int' to '*Card'. Could someone please tell me what i'm going wrong please. The rest of my class are having the same problem. Thank you.
Our assignment:
Pointers
This week you will add to last week’s assessment by writing a program that will deal two card hands.
Requirements
The requirements are that the main program must use a Card class to represent each card. You program must implement the following functions:
• createDeck – a function that creates the deck of cards
• dealCards – a function that fills up each hand of cards
• printHand(Card * hand[]) – a function that prints the hands contents and value to the screen
The main program will maintain an array variable of type Card that will represent the deck of cards. It will also maintain two arrays of type Card * (pointer to a Card) that will represent each hand. Each hand will be capable of holding five cards.
Your program will use the functions given above to create the deck and then deal the two hands with their cards. The dealing should be as random as possible so that a shuffle has been simulated. Then your program will print the contents and value of each hand.
Card.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include <iostream>
#include <string>
using namespace std;
//creates and holds the card suit and number.
class Card
{
private:
string Suit;
int Num;
public:
Card()
{
Num = 0;
}
Card(int n, string s)
{
Suit = s;
Num = n;
}
//returns the card number when called
int getNum()
{
return Num;
}
//returns the suit type when called
string getSuit()
{
return Suit;
}
string displayValue()
{
string v = "";
switch(getNum())
{
case 1:
v = "Ace"; break;
case 2:
v = "2"; break;
case 3:
v = "3"; break;
case 4:
v = "4"; break;
case 5:
v = "5"; break;
case 6:
v = "6"; break;
case 7:
v = "7"; break;
case 8:
v = "8"; break;
case 9:
v = "9"; break;
case 10:
v = "10"; break;
case 11:
v = "Jack"; break;
case 12:
v = "Queen"; break;
case 13:
v = "King";
}
return v + " of " + getSuit();
}
};
|
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include <iostream>
#include <string>
#include "class.cpp"
#include <ctime>
#include <cstdlib>
using namespace std;
Card*hand1[5];
Card*hand2[5];
Card card[52];
Card*hand[10];
int x=0,shuffle;
void createDeck()
{
int s, n;
string Suit;
//creates an array of 52 cards holding suit and number in the class
cout<<"Deck of Cards\n";
for (s=0;s<4;s++)
{
for(n=1;n<14;n++)
{
switch (s)
//sets the switch values to a string, saved to string Suit
{
case 0: Suit= "Hearts"; break;
case 1: Suit= "Clubs"; break;
case 2: Suit= "Diamonds"; break;
case 3: Suit= "Spades"; break;
}
card[x]= Card(n, Suit);
//this saves each card to the array
x++;
}
}
}
void dealCards()
{
srand(time(NULL));
int a;
for (a=0;a<10;a++)
{
shuffle=rand()%52;
hand[a]=shuffle; //This is the problem, this is obviously not the right
//way to do it.
}
}
void printHand(Card*hand[10])
{
}
int main()
{
createDeck();
dealCards();
return 0;
}
|