This is an exercise I am doing from C++ How to Program fourth edition by Deitel an Deitel.
The problem is to take a given program and have it deal a five hand card.
here is my code.
I am attempting to concatenate the suit and face number into a string called hand that I will later check for pairs, three of a kind etc, but the program crashes on me.Code:#include <iostream> using std::cout; using std::left; using std::right; using std::endl; #include <stdlib.h> #include <iomanip> using std::setw; #include <cstdlib> #include <ctime> void shuffle( int[][13]); void deal(char * [],const int[][13],const char*[],const char *[]); int main(int argc, char *argv[]) { //initialize suit array const char *suit[4]={"Hearts","Diamonds","Clubs","Spades"}; //initialize face array const char *face[13]={"Ace","Deuce","Three","Four","Five","Six", "Seven","Eight","Nine","Ten","Jack","Queen","King"}; //initialize deck array int deck[4][13]={0}; char * hand[5]={""}; srand(time(0)); shuffle(deck); deal(hand,deck,face,suit); system("PAUSE"); return 0; } //shuffle cards in deck void shuffle(int wDeck[][13]) { int row; int column; //for each of the 52 cards choose a slot of the deck randomly for (int card=1;card<=52;card++){ do{ row=rand()%4; column=rand()%13; }while (wDeck[row][column] != 0); wDeck[row][column]=card; } } //deal cards in deck void deal(char * thand[],const int wDeck[][13], const char *wFace[],const char *wSuit[]) { //for each of the 52 cards for (int card=1;card<=5;card++) //loop through the rows of the wDeck for (int row=0; row<=3;row++) //loop through the columns of the deck for current row for(int column=0;column<=12;column++) if(wDeck[row][column]==card){ strcat(thand[card],wFace[column]); strcat(thand[card],wSuit[row]); } } void showhand(char * thand[],const int wDeck[][13], const char *wFace[],const char *wSuit[]) { for (int card=1;card<=5;card++) cout << thand[card]<<endl; }
ignore



LinkBack URL
About LinkBacks


