Help, deck of cards

I am trying to set up a deck of cards that have two other arrays of type constant strings for ranks and values. I need to initialize the deck with two nested for loops and a seperate counter to assign each of the 52 structures in the deck a unique combination of rand and suit and value. I need to use the string arrays ranks and suits to initialize the card members rank and suit. The value member should be initialized to a value from 1 through 13 representing ace through king.

I need help I am so lost at the moment. Any would be great.

Thanks in advance


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

#include <iostream>
#include <cstdlib> //for rand and srand
#include <cstdio>
#include <string>


using namespace std;


#define pause cout << endl; system("pause")
#define ranks
#define suits	

	
	//structure definition
struct card

{
	string rank;//this example uses C++ string notation
	string suit;
	int value;
};

	

//main function
int main()
{
	int deck[52];  // An array of cards named deck, size 52
	

	string ranks[13] = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
        "Nine", "Ten", "Jack", "Queen", "King" };

	string suits[4] = { "Diamonds", "Hearts", "Spades", "Clubs" };
	

	int z[ 13 ];
	int k = 0; 

	for ( int i = 0; i < ranks; i++)
	{
		for ( int j = 0; j < suits; j++)
		{
			z[ k ] = ranks[ i ] * suits[ j ];
			k++;
		}
	}

	pause;

		return 0;

}
I got it figured out so far now I just need to loop it:

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
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <cstdlib> //for rand and srand
#include <cstdio>
#include <string>
#include <ctime> // time function for seed value

using namespace std;


#define pause cout << endl; system("pause")


	
	//structure definition
struct card

{
	string rank;//this example uses C++ string notation
	string suit;
	int value;
};

	

//main function
int main()
{
	srand(time(0));
	double playon, y, n;

	struct card deck[52];  // An array of cards named deck, size 52
	

	const string ranks[ ] = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
        "Nine", "Ten", "Jack", "Queen", "King" };

	const string suits[ ] = { "Diamonds", "Hearts", "Spades", "Clubs" };
	

	
	int k = 0; 

	for ( int i = 0; i < 13; i++)
	{
		for ( int j = 0; j < 4; j++)
		{
			deck[ k ].rank = ranks[ i ];
			deck[ k ].suit = suits[ j ];
			k++;
		}
	}

	int z = rand () % 52;

	cout << deck[ z ].rank << " of " << deck[ z ].suit << endl;
	cout << "Would you like another card? " << endl;
	cout << "Please enter y or n: " << endl;
	cin >> playon;

	if (playon == y){ 

		cout << deck[ z ].rank << " of " << deck[ z ].suit << endl;
	}
	if (playon == n){

		cout << "Thank you for playing!" << endl;
	}

	

	pause;

		return 0;

}
Last edited on
Topic archived. No new replies allowed.