C++ Programming I keep getting an error message and I can\'t figure out where it
ID: 3872052 • Letter: C
Question
C++ Programming
I keep getting an error message and I can't figure out where it is, any help would be appreiceated.
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;
}
// Here is the error message I am getting from visual studios for reference.
/* 1>------ Build started: Project: CardShuffle, Configuration: Debug Win32 ------
1> Driver.cpp
1>c:usersriandesktopcardshufflecardh.h(5): error C2011: 'Card' : 'class' type redefinition
1> c:usersriandesktopcardshufflecardh.h(5) : see declaration of 'Card'
1>c:usersriandesktopcardshuffledriver.cpp(17): error C2027: use of undefined type 'Card'
1> c:usersriandesktopcardshufflecardh.h(5) : see declaration of 'Card'
1> c:usersriandesktopcardshufflecardh.h(5) : see declaration of 'Card'
1>c:usersriandesktopcardshuffledriver.cpp(17): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== */
Explanation / Answer
1. The error code 1903 as I cna see from given program indicates tha you have used int i many times in loops and every time you are dynamically declaring the variable like "for(int i=0;i<n;i++)" that is taken as multiple declaration by visual C++. So please declare the iindex variable i,j and k previous to use and use same copy evrywhere.
2. second error that I can see is error code 2011 that is clearly indicating that you are trying to redefine that 'card' class. As in the program given by you there is no redefination so you should see whether a file with same name already existing the given folder or not where that header file is residing.
Please use the given work around your errors will be resolved.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.