Outputting garbage

Hello Thanks in advance for your interest.

I am trying to make a standard deck of 52 playing cards.
And below is the output that I want to create.
=================================
Initialized Deck:
0: AC 13: AD 26: AH 39: AS
1: 2C 14: 2D 27: 2H 40: 2S
2: 3C 15: 3D 28: 3H 41: 3S
3: 4C 16: 4D 29: 4H 42: 4S
4: 5C 17: 5D 30: 5H 43: 5S
5: 6C 18: 6D 31: 6H 44: 6S
6: 7C 19: 7D 32: 7H 45: 7S
7: 8C 20: 8D 33: 8H 46: 8S
8: 9C 21: 9D 34: 9H 47: 9S
9: TC 22: TD 35: TH 48: TS
10: JC 23: JD 36: JH 49: JS
11: QC 24: QD 37: QH 50: QS
12: KC 25: KD 38: KH 51: KS
====================================

and this is a part of my code that has a problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
static const unsigned short VALUE = 13;
static const unsigned short SUIT = 4;
static const std::string VALUES[VALUE] = { "Ace","2","3","4","5","6","7","8","9","Ten","Jack","Queen","King" };
static const std::string SUITS[SUIT] = { "Clubs","Diamonds","Hearts","Spades" };

 void load(std::string deck[], const unsigned short SIZE)
{
	for (unsigned short i = 0; i < SUIT; ++i)
	{
		for (unsigned short c = 0; c < VALUE; ++c)
		{
			deck[i*VALUE+c] = VALUES[c]+SUITS[i][0];
		}
	}
}


But It outputs like this
==========================
Initialized Deck:
0: AceC 13: AceD 26: AceH 39: AceS
1: 2C 14: 2D 27: 2H 40: 2S
2: 3C 15: 3D 28: 3H 41: 3S
3: 4C 16: 4D 29: 4H 42: 4S
4: 5C 17: 5D 30: 5H 43: 5S
5: 6C 18: 6D 31: 6H 44: 6S
6: 7C 19: 7D 32: 7H 45: 7S
7: 8C 20: 8D 33: 8H 46: 8S
8: 9C 21: 9D 34: 9H 47: 9S
9: TC 22: TD 35: TH 48: TS
10: JackC 23: JackD 36: JackH 49: JackS
11: QueenC 24: QueenD 37: QueenH 50: QueenS
12: KingC 25: KingD 38: KingH 51: KingS
=========================================================

So I want to display only the first letter of the values so that I can make it look just like the output example.
but if I make a change like below
1
2
3
4
5
6
7
8
9
10
11

 void load(std::string deck[], const unsigned short SIZE)
{
	for (unsigned short i = 0; i < SUIT; ++i)
	{
		for (unsigned short c = 0; c < VALUE; ++c)
		{
			deck[i*VALUE+c] = VALUES[c][0]+SUITS[i][0];
		}
	}
}


it outputs garbage like this
Initialized Deck:
0: ? 13: 2 26: ; 39: ]
1: 1 14: 4 27: 2 40: [
2: x 15: 6 28: 3 41: p
3: y 16: @ 29: ; 42: [
4: z 17: ! 30: ] 43: p
5: d 18: @ 31: 2 44: [
6: p 19: # 32: 1 45: 2
7: a 20: $ 33: 2 46: y
8: s 21: ^ 34: 1 47: 5
9: e 22: ^ 35: 6 48: d
10: ? 23: 1 36: ] 49: a
11: ; 24: 2 37: s 50: b
12: ^ 25: 5 38: , 51: '
=========================================================

Why is it like this ?

I am sorry I did my best trying to describe my problem clearly and shortly
but it still seems messy..
Please feel free to ask me questions if you don't understand my problem..
Thanks!
Last edited on
Your first case is meaningful because it is doing
string + char
and that is defined. Your second is trying to do
char + char
which isn't defined.

You have several choices. e.g.
First set deck[i*value+c] = VALUES[c][0] (because string = char will work)
then - in a second statement - add SUITS[i][0] to the result (because string+char works).
Alternatively use the substr member function on VALUES[c] to keep it as a string rather than char, or use string( VALUES[c][0] ) in the addition.
Last edited on
closed account (48T7M4Gy)
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
#include <iostream>

static const unsigned short VALUE = 13;
static const unsigned short SUIT = 4;

static const std::string VALUES[VALUE] = { "Ace","2","3","4","5","6","7","8","9","Ten","Jack","Queen","King" };
static const std::string SUITS[SUIT] = { "Clubs","Diamonds","Hearts","Spades" };

void load(std::string deck[], const unsigned short SIZE)
{
    for (unsigned short i = 0; i < SUIT; ++i)
    {
        for (unsigned short c = 0; c < VALUE; ++c)
        {
            deck[i*VALUE+c] = VALUES[c][0];
            deck[i*VALUE+c] += SUITS[i][0];
        }
    }
}

int main()
{
    std::string Deck_of_Cards[VALUE * SUIT];
    load(Deck_of_Cards, VALUE * SUIT);
    
    for (unsigned short i = 0; i < SUIT; ++i)
    {
        for (unsigned short c = 0; c < VALUE; ++c)
        {
            std::cout << Deck_of_Cards[i*VALUE+c] << ' ';
        }
        std::cout << '\n';
    }
    
    return 0;
}
AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC 
AD 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KD 
AH 2H 3H 4H 5H 6H 7H 8H 9H TH JH QH KH 
AS 2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS 
Program ended with exit code: 0
closed account (48T7M4Gy)
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
#include <iostream>
#include <iomanip>

static const unsigned short VALUE = 13;
static const unsigned short SUIT = 4;

static const std::string VALUES[VALUE] = { "Ace","2","3","4","5","6","7","8","9","Ten","Jack","Queen","King" };
static const std::string SUITS[SUIT] = { "Clubs","Diamonds","Hearts","Spades" };

void load(std::string deck[], const unsigned short SIZE)
{
    for (unsigned short i = 0; i < SUIT; ++i)
    {
        for (unsigned short c = 0; c < VALUE; ++c)
        {
            deck[i*VALUE+c] = VALUES[c][0];
            deck[i*VALUE+c] += SUITS[i][0];
        }
    }
}

int main()
{
    std::string Deck_of_Cards[VALUE * SUIT];
    load(Deck_of_Cards, VALUE * SUIT);
    
    int count = 0;
    for (unsigned short c = 0; c < VALUE; ++c)
    {
        for (unsigned short i = 0; i < SUIT; ++i)
        {
            std::cout
            << std::setw(3) << count++
            << ':'  << Deck_of_Cards[i*VALUE+c] << ' ';
        }
        std::cout << '\n';
    }
    
    return 0;
}


  0:AC   1:AD   2:AH   3:AS 
  4:2C   5:2D   6:2H   7:2S 
  8:3C   9:3D  10:3H  11:3S 
 12:4C  13:4D  14:4H  15:4S 
 16:5C  17:5D  18:5H  19:5S 
 20:6C  21:6D  22:6H  23:6S 
 24:7C  25:7D  26:7H  27:7S 
 28:8C  29:8D  30:8H  31:8S 
 32:9C  33:9D  34:9H  35:9S 
 36:TC  37:TD  38:TH  39:TS 
 40:JC  41:JD  42:JH  43:JS 
 44:QC  45:QD  46:QH  47:QS 
 48:KC  49:KD  50:KH  51:KS 
Program ended with exit code: 0
Last edited on
Thanks you all~ I really appreciate it!!
Topic archived. No new replies allowed.