468,513 Members | 1,854 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

Help with sorting hand of playing cards

Hi Guys,

I have all my code working except sort function .

I need to sort the cards in hand......and I am having hard time with that....

Any help would be really helpful ;)
Expand|Select|Wrap|Line Numbers
  1. class hand{
  2. private:
  3. string theSuits; // contains suits in order
  4. string theValues; // contains values in order
  5. int numcards;
  6. public:
  7. unsigned size;
  8. acard* handcards; // an array of cards
  9.  
  10.  
  11.  
  12. hand();
  13. ~hand();
  14. void sortit( unsigned size);
  15. void show();
  16. void shuffle();
  17. };
  18.  
  19. void hand::sortit(unsigned size) {
  20.    sort(handcards, handcards + size);
  21.    for ( int i=0; i < size; ++i){
  22.    cout << h.handcards[i].str<< " " ; }
  23.    cout << endl;
  24.  
  25. }
  26.  
  27.  
  28. int main()
  29. {
  30. int cardnum(0);
  31. string h[5];
  32. srand(time(0));
  33.  
  34. deck mydeck;
  35. mydeck.show();
  36. mydeck.shuffle();
  37. mydeck.show();
  38. hand myhand1;
  39. hand myhand2;
  40. hand myhand3;
  41. hand myhand4;
  42. mydeck.hands(myhand1, 6);
  43. //myhand1.sortit(6);
  44. mydeck.hands(myhand2, 6);
  45. mydeck.hands(myhand3, 6);
  46. mydeck.hands(myhand4, 6);
  47.  
  48.  
  49. system("pause");
  50. return 0;
  51. }
Jan 30 '08 #1
2 4498
Studlyami
464 Expert 256MB
Your trying to use a sort function from the algorithm header on an array of objects. If I'm guessing the sort algorithm doesn't know how to handle the objects that are in the array. Look up c++ sorting techniques (Bubble Sort, Selection Sort, Insertion Sort, ect.) on google and try using one of them on your array in a sort function (your own function).
Jan 30 '08 #2
weaknessforcats
9,207 Expert Mod 8TB
Where is your card???

Isn't a hand a container of cards??

Doesn't a card contain a suit and a value ??

So why isn't your hand a vector<Card> ???

There is a sort with the vector that you can use. You will need a) to write an operator< for your Card class or b) write a function that takes two Card arguments by reference and returns true if the first argument is less than the second argument.
Jan 30 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

10 posts views Thread by Johnny Snead | last post: by
33 posts views Thread by dembla | last post: by
5 posts views Thread by Sam | last post: by
17 posts views Thread by Umesh | last post: by
5 posts views Thread by cherriecheng88 | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.