Hi everyone, I'm new here and I've been having a lot of problems in my C++ class. I really want to learn how to program but my teacher doesn't make much sense.
Right now, I'm working on a project and I have a couple errors. If anyone could help me out I'd be extremely grateful!
It's supposed to shuffle a deck of cards and then draw a card, but I can't get the draw function to work.
//Aryan D & Alicia Lewis
//April 15, 2005
//Lab 10
//Have a deck of cards. Make a ll cards true, pick out a card randomly.
// This is our original work
// ___________________ & ______________________
#include <iostream>
#include <cstdlib>
using namespace std; //introduces namespace std
enum suit {CLUBS, DIAMONDS, HEARTS, SPADES};
enum rank {TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK,
QUEEN, KING, ACE};
int const NUM_SUITS = SPADES - CLUBS + 1;
int const NUM_RANKS = ACE - TWO + 1;
class card
{
public:
void set(suit s1, rank r1);
// sets the suit and rank to specific values
void print(ostream &f);
// prints the card
double get_suit();
double get_rank();
private:
void print_suit(ostream &f);
// prints the suit of the card
void print_rank(ostream &f);
// prints the rank of the card
suit s;
rank r;
};
class deck
{
public:
void shuffle();
// initializes the deck for dealing
double draw();
private:
bool in_deck[NUM_SUITS][NUM_RANKS];
};
int main( void )
{
suit t = HEARTS;
int tnum = int(t);
rank r = FIVE;
int rnum = int(r);
if (rnum == tnum)
cout << "r and t have the same value when converted to integers\n";
else
cout << "r and t do not have the same value when converted to integers\n";
// It would be nice if the following comparison were illegal, but it's not in C++
if (r == t)
cout << "Suit variable t is the same as rank variable r\n";
else
cout << "Suit variable t is not the same as rank variable r\n";
cout << "t converted to an integer is " << tnum << endl;
cout << "We will now set t to the value which comes before it in the enumeration list\n";
// The following statement seems reasonable, but doesn't compile.
// Comment it out to proceed.
// t = t - 1;
// The next statement does what we hoped the preceding one would do.
t = suit(int(t) - 1); // first cast t to an integer, do the subtraction,
// then cast it back to a suit
// Now we'll try to print the value of t, but the results will be unsatisfactory.
// You can't print enumeration types directly.
cout << "suit variable t has the value " << t << endl;
// But we DO have a way to print a suit IF that suit is part of a card.
card c;
c.set(t, TWO); // this sets card c to have suit equal to t (DIAMONDS)
// and rank of TWO
c.print(cout); // doesn't work perfectly because you need to write some
// extra code
deck d;
d.shuffle();
/*for (int i = 1; i <= 52; i++)
{
c = d.draw();
c.print(cout);
cout<<" ";
if (i % 4 ==0)
cout <<endl;
}
return 0;
}*/
void card::set(suit s1, rank r1)
{
s = s1;
r = r1;
}
void card::print_suit(ostream &f)
{
if (s == CLUBS)
f << "CLUBS ";
else if (s == DIAMONDS)
f << "DIAMONDS";
else if (s == HEARTS)
f << "HEARTS ";
else if (s == SPADES)
f << "SPADES ";
else
f << "Invalid suit value\n";
}
void card::print_rank(ostream &f)
{
if (r == TWO)
f << "TWO";
else if (r == THREE)
f << "THREE";
else if (r == FOUR)
f << "FOUR";
else if (r == FIVE)
f << "FIVE";
else if (r == SIX)
f << "SIX";
else if (r == SEVEN)
f << "SEVEN";
else if (r == EIGHT)
f << "EIGHT";
else if (r == NINE)
f << "NINE";
else if (r == TEN)
f << "TEN";
else if (r == JACK)
f << "JACK";
else if (r == QUEEN)
f << "QUEEN";
else if (r == KING)
f << "KING";
else if (r == ACE)
f << "ACE";
else
f << "Invalid rank value\n";
}
void card::print(ostream &f)
{
print_rank(f);
f << " of ";
print_suit(f);
}
void deck::shuffle()
{
in_deck [NUM_SUITS][NUM_RANKS] = true;
int i;
for(i=0; i<4; i++)
{
int a;
for(a=0; a<13; a++)
in_deck[i] [a]=true;
}
}
double card::get_suit()
{
return s;
}
double card::get_rank()
{
return r;
}
double deck::draw()
{
card c;
int s1;
int r1;
int card;
int ranknums = rand() % 13;
int suitnums = rand() % 4;
s1 = suit (suitnums);
r1 = rank (ranknums);
if (in_deck [s1][r1] == true)
{
in_deck [s1][r1] = false;
card = in_deck [s1][r1];
c.set(s1,r1);
return card;
}
else
deck::draw();
}
