Broken C string code

I am going through an old C++ book for fun and doing all the exercises to refresh some C++ concept I have forgotten or haven't used in a long time. So with that being said I am doing a simple card evaluating program and my program keeps crashing and I am unsure as to why. My guess is I am miss using some <cstring> commands but I can't see where I am missing something. The program crashes randomly and it appears to happen right after two hands have been dealt. FYI if I don't cout the hand it crashes everytime :(. Here is all the relevant code:

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
// in the main before call to deal
const char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};

const char *face[13] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
			                "Ten", "Jack", "Queen", "King"};
// ... end main

void deal(int wDeck[][13], const char *wFace[], const char *wSuit[])
{
     char hand[2][5][15] = {{""}};

	for(int handNumber = 0; handNumber < 2; handNumber++)
	{
		for(int cardNumber = 1; cardNumber <= 5; cardNumber++)
		{
			int row = rand() % 4;
			int column = rand() % 13;

			while(wDeck[row][column] == 0)
			{
				row = rand() % 4;
				column = rand() % 13;
			}

			wDeck[row][column] = 0;

			char description[14] = "";

			strcat(description, wFace[column]);
			strcat(description, " ");
			strcat(description, wSuit[row]);

			strcpy(hand[handNumber][cardNumber - 1], description);
			
			cout << hand[handNumber][cardNumber - 1] << endl;
		}

		cout << "=======================\n";
	}
}


The second cout << "=================\n" never runs so it appears to be somewhere above that, however the hand is displayed find using the cout statement. Any help would be greatly appreciated, also I am aware that some of my code could be dated as this book is fairly old. Thanks for the help!
Looks like the description array is too small. If the face is "Queen" and suit is "Diamonds".
5 (face) + 1 (space) + 8 (suit) + 1 (null terminator) = 15, so the description array needs to be at least 15 characters.
You may also want to consider replacing your "magic numbers" with constants, then use this constant instead of the "magic number". For example
1
2
3
const int Num_suits = 4;
const char *suit[Num_suits] = {"Hearts", "Diamonds", "Clubs", "Spades"};
Last edited on
Ah damn null terminator!! I miss counted my numbers because I thought 15 was long enough lol. Thanks Peter87!

To jbl:
I am aware that constants are better for larger programs and make the code easier to read, I usually don't make those mistakes in actual programs but still shouldn't skip it just because this is a personal exercise ;).

Thanks to you both
use this code to successfully compile.
I use time to randomness deal every compile

sorry for my english

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
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <cstring>
#include <ctime>

using namespace std;

void deal(int wDeck[][13], const char *wFace[], const char *wSuit[]);

// in the main before call to deal
const char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};

const char *face[13] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
			                "Ten", "Jack", "Queen", "King"};
// ... end main

int main (int argc,char ** argv)
{
	time_t t= time(NULL);

	int wDeck[4][13];
	srand(t);
	for (int i = 0; i < 4; i++){
		for (int j=0; j < 13; j++){
			wDeck[i][j] = rand();
		}
	}
	deal(wDeck, face, suit);
	return 0;
}

void deal(int wDeck[][13], const char *wFace[], const char *wSuit[])
{
     char hand[2][5][15] = {{""}};

	for(int handNumber = 0; handNumber < 2; handNumber++)
	{
		for(int cardNumber = 1; cardNumber <= 5; cardNumber++)
		{
			int row = rand() % 4;
			int column = rand() % 13;

			while(wDeck[row][column] == 0)
			{
				row = rand() % 4;
				column = rand() % 13;
			}

			wDeck[row][column] = 0;

			char description[14] = "";


			strcat(description, wFace[column]);
			strcat(description, " ");
			strcat(description, wSuit[row]);

			strcpy(hand[handNumber][cardNumber - 1], description);

			cout << hand[handNumber][cardNumber - 1] << endl;
		}

		cout << "=======================\n";
	}
}
Last edited on
Topic archived. No new replies allowed.