Function is Overreacting

I've created a function that creates a new deck for a Blackjack game I'm making. When I had the algorithm in the main function it worked but when moved to a function problems started to sprout up. It's hard to explain the problem but it's a short code so I'm sure compiling it won't be a problem.

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
#include <iostream>

unsigned int new_deck();

int main()
{
    using namespace std;
    
    unsigned int Cards[52];
    int i;
    
    Cards[52] = new_deck();
    
    for (i = 0; i < 52; i++)
    cout << Cards[i];
    
    
    cout << "\nPress ENTER to exit\n";   
    cin.get();
    cin.get();
    return 0;
}
          
unsigned int new_deck()
{
    unsigned int cards[52];
    int i, cardValue = 2;
    
    for (i = 0; i < 52; i++)
    {
          if (cardValue > 11 && i < 40)
          cardValue = 2;
          else if (i == 40)
          cardValue = 10;
          
          cards[i] = cardValue;
          if (i < 40)
          cardValue++;
    }
    return cards[52];
}


I haven't returned an array before so there may be something I'm doing wrong. Any help is appreciated.
Last edited on
That function doesn't return an array. It returns an unsigned int at position 52 in the array.

Position 52 is also outside the array bounds which go from 0 to 51 (52 in total).

If you want to fill an array like that you could do this:

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
#include <iostream>

void new_deck(unsigned int Cards[52]);

int main()
{
    using namespace std;

    unsigned int Cards[52];
    int i;

    new_deck(Cards);

    for (i = 0; i < 52; i++)
    cout << Cards[i];


    cout << "\nPress ENTER to exit\n";
    cin.get();
    cin.get();
    return 0;
}

void new_deck(unsigned int cards[52])
{
    int i, cardValue = 2;

    for (i = 0; i < 52; i++)
    {
          if (cardValue > 11 && i < 40)
          cardValue = 2;
          else if (i == 40)
          cardValue = 10;

          cards[i] = cardValue;
          if (i < 40)
          cardValue++;
    }
}
Last edited on
Thanks for the quick reply and lightly altered code :)
Topic archived. No new replies allowed.