Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Programming I keep getting an error message and I can\'t figure out where it

ID: 3872290 • Letter: C

Question

C++ Programming

I keep getting an error message and I can't figure out where it is.

Here is the question.

Card Shuffling and Dealing

Create a program to shuffle and deal a deck of cards. The program should consist of class Card, class DeckOfCards and a driver program. Class Card should provide:

Private data members face and suit of type int.

A constructor that receives two ints representing the face and suit and uses them to initialize the data members.

Two static arrays of strings representing the faces and suits.

A toString function that returns the Card as a string in the form "face of suit." You can use the + operator to concatenate strings.

Class DeckOfCards should contain:

An array of Cards named deck to store the Cards.

An integer currentCard representing the next card to deal.

A default constructor that initializes the Cards in the deck.

A shuffle function that shuffles the Cards in the deck. The shuffle algorithm should iterate through the array of Cards. For each Card, randomly select another Card in the deck and swap the two Cards.

A dealCard function that returns the next Card object from the deck.

A moreCards function that returns a bool value indicating whether there are more Cards to deal.

The driver program should create a DeckOfCards object, shuffle the cards, then deal the 52 cards.

Create a PokerHand class.

It should have a way to represent a 5 card poker hand as a private data member.

There should be a constructor method that randomly creates a 5 card poker hand from a DeckOfCards.

It should also have a method to set the poker hand to a particular 5 card hand that the programmer specifies.

It should have methods that return booleans and take in 0 arguments to accomplish each of the following:

Determine whether the hand contains a pair.

Determine whether the hand contains two pairs.

Determine whether the hand contains three of a kind (e.g., three jacks).

Determine whether the hand contains four of a kind (e.g., four aces).

Determine whether the hand contains a flush (i.e., all five cards of the same suit).

Determine whether the hand contains a straight (i.e., five cards of consecutive face values).

Add your driver program the following.

Create a randomly generated PokerHand using your constructor and output the results of your PokerHand methods that determine what type of hand it has.

You should also create other PokerHands and set them to have a pair, two pair, three of a kind, four of a kind a flush and a straight respectively.

Use const where appropriate. I/O should be via the console

And here is my code.

Card.cpp

#include <iostream>
#include <cstdio>
#include "CardH.h"
#include <string>

using namespace std;

string Card::suits[4] = {"Diamonds","Clubs","Hearts","Spades"};
string Card::faces[13] = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine",
   "Ten","Jack","Queen","King"};

Card::Card(int type,int num)
{
   suit = suits[type];
   face = faces[num];
}

void Card::toString()
{
   string printout = face + " of " + suit;
   cout << printout << endl;
}

string Card::getFace()
{
   return face;
}

string Card::getSuit()
{
   return suit;
}


______________________________________________________
CardH.h

#include <vector>
#include <string>
using namespace std;

class Card {
private:

   string face;
   string suit;

public:

   static string faces[13];
   static string suits[4];
   string getFace();
   string getSuit();
   Card(int, int);
   void toString();
};


________________________________________

DeckOfCardsH.h

#include <vector>
#include "CardH.h"


class DeckOfCards {
private:
   vector<Card>deck;
   int currentCard;
   void swap(int, int);

public:
   DeckOfCards();
   void shuffle();
   Card dealCard();
   bool moreCards();
};


___________________________________________________

DeckOfCards.cpp

#include <iostream>
#include <time.h>
#include <cstdlib>
#include "DeckOfCardsH.h"

DeckOfCards::DeckOfCards() {
   currentCard = 0;
   for( int i=0; i<4; i++){

       for(int j=0; j<13; j++){

           Card c = Card(i,j);
           deck.push_back(c);
       }
   }

}

void DeckOfCards::shuffle(){
   srand(time(NULL));
   for(int i=0; i<52; i++)
   {
       int s = rand()%52;
       swap (i,s);
   }
}

Card DeckOfCards::dealCard()
{
   currentCard++;

   if (moreCards())
   {
       return deck[currentCard - 1];
   }
   else
   {
       cout << "The deck is empty " << endl;
   }
}

bool DeckOfCards::moreCards()
{

   if (currentCard < 52)
   {
       return true;
   }
   else
   {
       return false;
   }

}

__________________________________________________

Driver.cpp

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include "cardH.h"
#include "DeckOfCardsH.h"

using namespace std;

int main(int argc, char *argv[]) {
DeckOfCards d = DeckOfCards();
vector<Card>hand;

cout << "The cards you have been delt are: " << endl;

for (int i=0;i<5;i++)
{
   hand.push_back(d.dealCard());
       hand[i].toString();
}

int flag = 0;

for (int i=0;i<5;i++)
{
   for (int j= i+1;j<5;j++)
   {
       if ((hand[i].getFace()).compare(hand[j].getFace()) == 0 && i !=j)
       {
           flag++;
       }
   }
}

if(flag == 1)
{
   cout << "You have been delt a pair! " << endl;
}

if (flag > 1)
{
   cout << "You have been delt two pair! " << endl;
}

flag = 0;

for (int i=0;i<5;i++)
{
   for (int j= i+1;j<5;j++)
   {
       for (int k= j+1; k<5;k++)
       {

       if ((hand[i].getFace()).compare(hand[j].getFace()) == 0 &&
           (hand[i].getFace()).compare(hand[k].getFace()) == 0 && i !=j != k)
       {

           flag++;
       }
      
       }

   }
}

if (flag)
{
   cout << "You have been delt Three of a kind! " << endl;
   flag = 0;
}

if
   ((hand[0].getSuit()).compare(hand[1].getSuit()) == 0 &&
   (hand[0].getSuit()).compare(hand[2].getSuit()) == 0 &&
   (hand[0].getSuit()).compare(hand[3].getSuit()) == 0 &&
   (hand[0].getSuit()).compare(hand[4].getSuit()) == 0)
{
      
       cout << "You have been delt a flush! " << endl;

vector<int>v;
   for (int i=0;i<5;i++)
   {

       for (int j=0;j<13;j++)
       {

               if ((hand[i].getFace()).compare(hand[i].faces[j]) == 0)
                  
               {
                   v.push_back(j+1);
               }
       }
   }
   sort(v.begin(),v.end());
   if (v[0] == v[1]-1 && v[0] == v[2]-2 && v[0] == v[3]-3 && v[0] == v[4]-4)
   {
       cout << "You have been delt a straight! " << endl;
   }
}


system ("pause");
return 0;

}

Explanation / Answer

==================Card.h====================

/*
*
* Card.h
*
*/

#ifndef CARD_H_
#define CARD_H_

#include <string>

/*
*
*/
class Card {
private:
int face;
int suit;
public:
static const std::string faces[];
static const std::string suits[];
Card();
Card(int suit, int face);
virtual ~Card();
std::string toString();
};
#endif /* CARD_H_ */

=============================Card.cpp-=======================

/*
*
* Card.cpp
*
*/

#include "Card.h"

const std::string Card::faces[] = {"Ace", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"Jack", "Queen", "King"};
const std::string Card::suits[] = {"Spade", "Heart", "Diamond", "Club"};

Card::Card(){
face = 0;
suit = 0;
}
Card::Card(int suit, int face) {
this->face = face;
this->suit = suit;
}

Card::~Card() {
// TODO Auto-generated destructor stub
}
/*
* String representation of the Card
*/
std::string Card::toString(){
return Card::faces[face] + " of " + Card::suits[suit];
}

==========================DeckOfCard.h================================

/*
*
* DeckOfCards.h
*
*/

#ifndef DECKOFCARDS_H_
#define DECKOFCARDS_H_

#include "Card.h"

/*
*
*/
class DeckOfCards {
private:
Card deck[52];
int currentCard;
public:
DeckOfCards();
void shuffleCards();
Card dealCard();
bool moreCards();
};

#endif /* DECKOFCARDS_H_ */

==================================DeckOfCards.cpp==============================

/*
*
* DeckOfCards.cpp
*
*/
#include <stdlib.h>
#include "DeckOfCards.h"
/**
* Default Constructor for DeckOfCards. Initializes all cards
*/
DeckOfCards::DeckOfCards() {
for( int i=0; i<4; i++)
for(int j=0; j<13; j++)
deck[i*13+j] = Card(i, j);
currentCard = 0;
}
/**
* Shuffles cards in the deck;
*/
void DeckOfCards::shuffleCards(){
for(int i=0; i<52; i++){
int rnd = rand()%52;
Card temp = deck[i];
deck[i] = deck[rnd];
deck[rnd] = temp;
}
}

Card DeckOfCards::dealCard() {
return deck[currentCard++];
}

bool DeckOfCards::moreCards(){
return currentCard != 52;
}

================================Driver.cpp==========================

//============================================================================
// Name : Driver.cpp
// Author :
// Version :
// Copyright :
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <iomanip>
#include "card.h"
#include "DeckOfCards.h"

using namespace std;

int main() {
DeckOfCards doc;
doc.shuffleCards();

int j=0;
while (doc.moreCards()) {
Card c = doc.dealCard();
cout << setw(20) << c.toString();
if (++j % 4 == 0) {
j=0;
cout << endl;
}
}
}

Card.h:

#ifndef CARD_H
#define CARD_H
#include<string>
using namespace std;

class Card
{
private:
int iFace, iSuit;
//static const char *suit[4];
//static const char *face[13];
static string suit[4];
static string face[13];
public:
Card (int = 0, int = 0);
string toString() const;
char getSuitLetter() const;
short getCardNum() const;

};
#endif

DeckOfCards.h:

#ifndef DECK_H
#define DECK_H
#include "card.h"
#include<iostream>
#include <vector>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;

class DeckOfCards
{
private:
int currentCard;
int lastCard;
vector<Card>deck;
void initializeDeckOfCards();
void printCard();
public:
DeckOfCards();
void shuffle();
Card dealCard();
bool moreCards() const;
   int getNumDealed() const;
void printDeckOfCards(int, int);
};
#endif

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote