Inexplicable answer in card shuffle program

So, the card shuffle program works like this: create an array of 52 cards (first letter is suit, followed by rank in number or letter). 10=T. The input is 52 "random" numbers which I have to take the remainder after dividing 52, and then swap that random position's card with the first card, then get second random position with the second card, then.... (etc.) However I got a REALLY weird output that I don't really get from this program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
using namespace std;
int main(){
    /*ranks: A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K
suits: C, D, H, S*/
string cards [52] = {"CA", "C2", "C3","C4", "C5","C6","C7","C8","C9","CT","CJ","CQ","CK",
"DA", "D2", "D3","D4", "D5","D6","D7","D8","D9","DT","DJ","DQ","DK",
"HA", "H2", "H3","H4", "H5","H6","H7","H8","H9","HT","HJ","HQ","HK",
"SA", "S2", "S3","S4", "S5","S6","S7","S8","S9","ST","SJ","SQ","SK"};
int card, num;
for (int original=0; original<52;original++){
cin>>card;
int again = card % 52;
   swap(cards[original],cards[again]);}
   cout<<cards<<" ";
}


Output: 0x7ffd8cf66280
Huh?

A do while loop has a similar output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
using namespace std;
int main(){
    /*ranks: A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K
suits: C, D, H, S*/
string cards [52] = {"CA", "C2", "C3","C4", "C5","C6","C7","C8","C9","CT","CJ","CQ","CK",
"DA", "D2", "D3","D4", "D5","D6","D7","D8","D9","DT","DJ","DQ","DK",
"HA", "H2", "H3","H4", "H5","H6","H7","H8","H9","HT","HJ","HQ","HK",
"SA", "S2", "S3","S4", "S5","S6","S7","S8","S9","ST","SJ","SQ","SK"};
int card, num;
int original = 0;
do{cin>>card;
int again = card % 52;
   swap(cards[original],cards[again]);
original ++;
} while (original<52);
   cout<<cards<<" ";}


Output: 0x7ffe7c1c6320

Sample input: 274 85 79 31 7070 3864 23 352 4140 7486 899 0 3927 959 3213 54 418 892 42 88 98 38 85 565 55 7064 407 2774 1728 68 333 9 5979 9530 19 1497 9 378 2555 201 2395 2546 614 446 7680 28 33 852 3518 9526 634 71
Last edited on
When you do: cout << cards you're feeding cout a pointer. And, by default, cout displays the address contained in the pointer, which is what you are seeing.

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
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
    /*ranks: A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K
    suits: C, D, H, S*/
    string cards[52] = { "CA", "C2", "C3","C4", "C5","C6","C7","C8","C9","CT","CJ","CQ","CK",
        "DA", "D2", "D3","D4", "D5","D6","D7","D8","D9","DT","DJ","DQ","DK",
        "HA", "H2", "H3","H4", "H5","H6","H7","H8","H9","HT","HJ","HQ","HK",
        "SA", "S2", "S3","S4", "S5","S6","S7","S8","S9","ST","SJ","SQ","SK" };
    int card, num;
    for (int original = 0; original<52; original++) {
        cin >> card;
        int again = card % 52;
        swap(cards[original], cards[again]);
    }

    for (auto card : cards)
        std::cout << card << ' ';
    // or:
    std::cout << '\n';
    for (std::size_t i = 0; i < 52; ++i)
        std::cout << cards[i] << ' ';
}
thanks man. Just curious, what does "auto" do?
auto lets the compiler deduce the type.

In the for loop, the type of card will be std::string.
hmmm... interesting alternative solution with vector and also with 'auto':
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
#include <iostream>
#include <string>
#include <vector>

const char ranks[] = {'A','2','3','4','5','6','7','8','9','T','J','Q','K'};
const char suits[] = {'C','D','H','S'};

int main()
{
    std::vector<int> deck;
    deck.reserve(52);
    for (int i=0; i<52; i++)
        deck.push_back(i);

    int step = 0;
    while (std::cin.good()) {
        int n=0;
        std::cin >> n;
        std::swap(deck[step%52], deck[n%52]);
        step++;
    }

    for (auto n : deck) {
        std::cout << suits[n/13] << ranks[n%13] << " ";
    }
    std::cout << std::endl;

}
Topic archived. No new replies allowed.