can someone help me finish this code.

well im having a hard time with this code. mainly because of the pointers. i know what i need to do but its hard for me to translate it to codes. so i hope someone could help me.

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include<iostream>
using namespace std;
#include<vector>; 


class CardDeck
{
  public:
        CardDeck();   // default constructor
	~CardDeck();  // destructor

/*The constructor should create 52 cards individually (no loops) 
using the new operator (dynamic memory) and add them to the deck vector*/
	CardDeck::CardDeck(){

		//HEART suit
		deck.push_back(new Card(ACE, HEARTS));
		deck.push_back(new Card(TWO, HEARTS));
		deck.push_back(new Card(THREE, HEARTS));
		deck.push_back(new Card(FOUR, HEARTS));
		deck.push_back(new Card(FIVE, HEARTS));
		deck.push_back(new Card(SIX, HEARTS));
		deck.push_back(new Card(SEVEN, HEARTS));	
		deck.push_back(new Card(EIGHT, HEARTS));
		deck.push_back(new Card(NINE, HEARTS));
		deck.push_back(new Card(TEN, HEARTS));
		deck.push_back(new Card(JACK, HEARTS));
		deck.push_back(new Card(QUEEN, HEARTS));
		deck.push_back(new Card(KING, HEARTS));
		
		//CLUBS suit
		deck.push_back(new Card(ACE, CLUBS));
		deck.push_back(new Card(TWO, CLUBS));
		deck.push_back(new Card(THREE, CLUBS));
		deck.push_back(new Card(FOUR, CLUBS));
		deck.push_back(new Card(FIVE, CLUBS));
		deck.push_back(new Card(SIX, CLUBS));
		deck.push_back(new Card(SEVEN, CLUBS));		
		deck.push_back(new Card(EIGHT, CLUBS));
		deck.push_back(new Card(NINE, CLUBS));
		deck.push_back(new Card(TEN, CLUBS));
		deck.push_back(new Card(JACK, CLUBS));
		deck.push_back(new Card(QUEEN, CLUBS));
		deck.push_back(new Card(KING, CLUBS));
		
		//SPADES suit
		deck.push_back(new Card(ACE, SPADES));
		deck.push_back(new Card(TWO, SPADES));
		deck.push_back(new Card(THREE, SPADES));
		deck.push_back(new Card(FOUR, SPADES));
		deck.push_back(new Card(FIVE, SPADES));
		deck.push_back(new Card(SIX, SPADES));
		deck.push_back(new Card(SEVEN, SPADES));		
		deck.push_back(new Card(EIGHT, SPADES));
		deck.push_back(new Card(NINE, SPADES));
		deck.push_back(new Card(TEN, SPADES));
		deck.push_back(new Card(JACK, SPADES));
		deck.push_back(new Card(QUEEN, SPADES));
		deck.push_back(new Card(KING, SPADES));

		//DIAMONDS suit
		deck.push_back(new Card(ACE, DIAMONDS));
		deck.push_back(new Card(TWO, DIAMONDS));
		deck.push_back(new Card(THREE, DIAMONDS));
		deck.push_back(new Card(FOUR, DIAMONDS));
		deck.push_back(new Card(FIVE, DIAMONDS));
		deck.push_back(new Card(SIX, DIAMONDS));
		deck.push_back(new Card(SEVEN, DIAMONDS));		
		deck.push_back(new Card(EIGHT, DIAMONDS));
		deck.push_back(new Card(NINE, DIAMONDS));
		deck.push_back(new Card(TEN, DIAMONDS));
		deck.push_back(new Card(JACK, DIAMONDS));
		deck.push_back(new Card(QUEEN, DIAMONDS));
		deck.push_back(new Card(KING, DIAMONDS));
	}

	CardDeck::~CardDeck(){
	//while deck is not empty
		while(deck.back > 0)
		{ delete Card;
		}
		//call delete on the last pointer in the deck to free the card
		//remove that pointer from the deck vector

	}

	/* Returns the Card pointer at the back of the vector or NULL if the deck is empty. */
	Card* getTopCard();
	Card*CardDeck::getTopCard();
	
	//need a local card* variable to hold the pointer to the top of the deck call it top
	//set it to null
	//if deck is not empty
	//store last card* in deck into top
	//remove that pointer from deck vector
	//return top
	 int * top = deck.front;


        /* Prints each card, one per line, using the Card's toString() member function. */
	void print()const;

	void CardDeck::print()const
	{
		/*loop through the deck
		for each element of deck
		dereference card pointer
		print out ots tostring()
		*/
		//loop to dereference card pointer.
	}

        /* Shuffles the deck of cards by swapping two random elements of the deck. */
	void shuffle();
	void CardDeck::shuffle()
	{
	/*1000 times
	swap 2 random elements of vector
	
	*/
	}



  private:
        vector<Card*> deck;

        /* Swaps the contents of the elements of deck at the given indices. */
        void swapCards(int index1, int index2);
};


//The Card class has already been defined for you:

enum Suit {HEARTS, CLUBS, SPADES, DIAMONDS};
enum Rank {ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING};

class Card{

  public:
          Card(Rank r, Suit s);
          Rank getRank()const;
          Suit getSuit()const;
          string toString()const;

  private:
	  Suit suit;
	  Rank rank;
};

Card::Card(Rank r, Suit s){
   suit = s;
   rank = r;
}

Rank Card::getRank()const{
   return rank;
}

Suit Card::getSuit()const{
   return suit;
}

string Card::toString()const{
  
  string description = "";
  
  switch(rank) {
	case ACE:
		description += "Ace";
		break;
	case TWO:
		description += "Two";
		break;
        case THREE:
		description += "Three";
		break;
	case FOUR:
		description += "Four";
		break;
        case FIVE:
		description += "Five";
		break;
	case SIX:
		description += "Six";
		break;
        case SEVEN:
		description += "Seven";
		break;
	case EIGHT:
		description += "Eight";
		break;
	case NINE:
		description += "Nine";
		break;
        case TEN:
		description += "Ten";
		break;
	case JACK:
		description += "Jack";
		break;
	case QUEEN:
		description += "Queen";
		break;
	case KING:
		description += "King";
		break;
  }

  description += " of ";

  switch(suit) { 
	case HEARTS:
		description += "Hearts";
		break;
	case CLUBS:
                description += "Clubs";
		break;
	case SPADES:
		description += "Spades";
		break;
	case DIAMONDS:
                description += "Diamonds";
		break;
  }
  return description;
}


int main(){

	CardDeck d;
	d.print();

	cout << "Shuffling..." << endl << endl;

	d.shuffle();
    
	d.print();

	cout << endl << endl;
 
        return 0;
}

Last edited on
You don't have to store the cards as pointers. Actually I don't see much point in doing so to be honest. Try to avoid pointers if you don't have to use them.

For shuffle you can use std::random_shuffle in <algorithm>.
thanks for taking the time to look at the code, but i have to use pointer and vectors since is the topic/section we are reviewing in class. i have read the tutorial on vector and pointer but i dont really know how to apply it to this problem.

i know it is a assigment so im not really asking for the answer. i just want some tips or examples on how to do it.
Last edited on
first of all I like how organized your code is, sometimes reading other people's code is the biggest problem. Second... try not to build your functions in the classes. What I want to see is a class that shows only what I need to know so I can use it. I am not so concerned with the implementation (which you can put below main). Is this code just unfinished because is see deck.push_back (information) but i dont see the declaration of the vector deck. What is it? A queue, a deque, a stack? Also what is the main purpose of this program?
its under private.
1
2
private:
        vector<Card*> deck;


the purpose is to get some practice with vector, pointers and classes. as for the program the only purpose is to shuffle the cards and print them on the screen one by one.
what that is basically saying is make an array that can only store pointers that point to the object card and name it deck. If you look at this site here you can get some of the methods for a vector: http://www.cplusplus.com/reference/stl/vector/vector/

I help you with one method in your class and see how you do from there.
-line 89 the getTop() method. On line 97 you have:
int * top = deck.top();
What you are saying there is create a pointer to an integer and make it point to whatever the pointer of the Deck is pointing to. This wont work because you have a type mismatch.
int != Deck. So you will want to do something like this:
Deck* top = this->back();
This code initializes a Deck pointer and sets it equal to the back of the vector (which is your top since a vector can only remove from the back).
The 'this' pointer just references the calling object so if in main you say:
1
2
 Deck d;
d.getTop();

The 'this' pointer will reference (or point to) the object 'd' that called the function. The arrow operator ("->") is a easy way to dereference the 'this' pointer (or any pointer) but you can do it two ways "this->pop_back()" or "(*this).pop_back();"
Next, you'll want to get rid of the 'top' element. You do this by:
this->pop_back();
This calls your Deck destructor to destroy the object and changes the value of top to the next element in line. So when you get all done you should have something like this (I just filled in the easy parts):
1
2
3
4
5
6
7
Deck* top = NULL;
if (!this->empty())
{
top = this->back();
this->pop_back();
}
return top;


this returns top pointer, you can do this because of the declaration of the class: Deck* Deck :: getTop()
the first Deck denotes the return type and with the astrisk, it means it will return a Deck pointer. Then the second deck is just the class with the scope reesolution operator and the function name. Anyway, hope that helps... im going to go grab some grub. Let me know if you need more help.
Thanks ill work ont the codes as soon as i get home.. ill try to work from there
im kindda confussed with "this" pointer. but i guess i will read about it. one more thing how do u use tostring();?
1
2
3
4
5
6
7
8
9
10
11
12
 /* Prints each card, one per line, using the Card's toString() member function. */
	void print()const;

	void CardDeck::print()const
	{
		/*loop through the deck
		for each element of deck
		dereference card pointer
		print out ots tostring()
		*/
		//loop to dereference card pointer.
	}

and what is the best way to shuffle random elements of a vector?
No worries. The 'this' pointer is a pointer that is automatically generated when an object calls one of its member function. So it is always available to you in that sense. I use it just to help differentiate the calling objects variables from other variables in the method.

Whats really nice is that toString() is already written for you. Notice this line of code on line 125 and 126:
1
2
private:
        vector<Card*> deck;

This private member variable creates a vector that can only hold card objects and it is named deck. So in each element of the vector (or array if you prefer) you have a pointer to a card object. Well if you take a look at the card objects methods you see a toString() method. So this tells you that each card object has a toString() method. The toString() method generally formats text into a way that is meaningful to you (because you write it). There are many different ways to do what the toString() method does though (I.E. overloading the extraction operator (<<), or just accessing each of the objects member variables individually and cout << them. The reason why a toString() method or overloaded extraction operator is preferred is because the member variables of your class is private, so they are not directly accessible from non-member functions (such as main). Another reason is because of modulation. Modulation is just basically grouping code that is similar into a function so it can be easily called, easily manageable, and you don't have to keep writing the same code over and over again. Whats nice about object oriented programming is abstraction. All you need to know about toString() is how to call it, what arguments does it take, and what does it return. You are not concerned with how it does it.

toStrings() can be accessed via the dot operator (.) or if you are trying to access it through a pointer you will first have to dereference the pointer. The two ways to do this is the arrow operator (->) like I used with the 'this' pointer or like this (*pointer to object) then using the dot operator after that.

As for your question about vectors. The STL (Standard Template Library) contains three things, 1) containers (such as a queue or vector), 2) iterators (your instructor will probably explain that to you), and 3) generic algorithms. In the generic algorithms section of the STL you can find a nice little method that will shuffle your vector. The link to that is:

http://www.cplusplus.com/reference/algorithm/

if you googled that last question you have you could have found that out.
thanks for your help i was able to finish the program. i mean is kindda useless since it only display the deck and suffles it. but it was good for practice. and thanks again.
Not entirely useless. A few more codes and you could turn it into a blackjack game or something. Your welcome.
Topic archived. No new replies allowed.