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 ;)

#include <algorithm>
#include <iostream>
#include <string>
#include <stdexcept>
#include <exception>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>

using namespace std;


struct acard{
public:
int suit;
int value;
string str;
unsigned int index;
bool operator <(const acard& rhs)const{
return index < rhs.index;
}
};

class hand{
private:
string theSuits; // contains suits in order
string theValues; // contains values in order
int numcards;
public:
unsigned size;
acard* handcards; // an array of cards



hand();
~hand();
void sortit( unsigned size);
void show();
void shuffle();
};

hand::hand():
//theSuits("\x05\x04\x03\x06")
theSuits("\C\D\H\S")
,theValues("23456789TJKQA")
,numcards(size)
,handcards(new acard[numcards])
{

}

hand::~hand(){
delete [] handcards;
}

class deck{
private:
string theSuits; // contains suits in order
string theValues; // contains values in order

public:

int numcards;
acard* deckofcard; // an array of cards



deck();
~deck();
void show();
void hands(hand h, unsigned size);
void shuffle();
};

deck::deck():
//theSuits("\x05\x04\x03\x06") //c, d, h, s
theSuits("\C\D\H\S")
,theValues("23456789TJKQA")
,numcards(theSuits.length() * theValues.length())
,deckofcard(new acard[numcards])
{
int cardnum(0);
for(int i = 0; i < theSuits.length(); ++i){
for(int j = 0; j < theValues.length(); ++j){
cardnum = (i * theValues.length()) + j;
deckofcard[cardnum].suit = i;
deckofcard[cardnum].value = j;
deckofcard[cardnum].str = theValues.substr(j,1) +
theSuits.substr(i,1); }
}
}

deck::~deck(){
delete [] deckofcard;

}

void deck::show(){
for(int i=0; i < numcards; ++i){
cout << (i % 13 ? ' ' : '\n') << deckofcard[i].str ;
}
cout << endl;
cout << endl;
}

void deck::shuffle(){
for(int i=0; i < numcards; ++i){
deckofcard[i].index = rand();
}
sort(deckofcard, deckofcard + numcards); }

void deck::hands(hand h, unsigned size){
cout << endl;
for ( int i=0; i < size; ++i){
numcards--;
h.handcards[i].str = deckofcard[numcards].str;

cout << h.handcards[i].str << " " ; }
//sort(h.handcards, h.handcards + size);
cout << endl;
h.sortit( size);
}

void hand::sortit(unsigned size) {
sort(handcards, handcards + size);
for ( int i=0; i < size; ++i){
cout << h.handcards[i].str<< " " ; }
cout << endl;

}


int main()
{
int cardnum(0);
string h[5];
srand(time(0));

deck mydeck;
mydeck.show();
mydeck.shuffle();
mydeck.show();
hand myhand1;
hand myhand2;
hand myhand3;
hand myhand4;
mydeck.hands(myhand1, 6);
//myhand1.sortit(6);
mydeck.hands(myhand2, 6);
mydeck.hands(myhand3, 6);
mydeck.hands(myhand4, 6);


system("pause");
return 0;
}

any guidence......would be really helpful

Guidance #1: Don't bump your thread ten minutes after you post. Most of us have lives ouside of this forum
Guidance #2: Learn to format your code. I for one refuse to read code that I can't follow because of improper formatting.
Guidance #3: Read THE RULES as you were asked when you registered. It includes CODE tags.
Guidance #4: Check out the sticky posts at the top of the forum for additional helpful tidbits of information. Obvious posts will stick out.

agreed with WaltP

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.