Structs and Arrays

This project for school is focused on using structs and arrays. I have to make the game "spades" using structs and arrays in my code. Right now I'm trying to create a deck and shuffle the cards.

Below is my current code. When I run this code, it displays numbers 50 all the way to -1. I'm not sure why this is happening.

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

using namespace std;



struct SOneCard
{
	int iRank;
	enum eSuit {CLUBS, DIAMONDS, HEARTS, SPADES};
	eSuit Suits;
};

struct SCardDeck
{
	SOneCard aiDeck[52];
	int iCardIndex;
	 
};

struct SPlayerHand
{
	SOneCard aiHand[13];
	int iTotalHand;
};

int main(int argc, char* argv[])
{
	char playAgain;

	SOneCard Suits;
	SCardDeck Deck;
	
	Deck.iCardIndex = 51;

	for ( int i = 2; i <= 14; i++)
	{
		for (int j = 0; j <= 3; j++)
		{
			Deck.aiDeck[Deck.iCardIndex].iRank = i;
			Deck.aiDeck[Deck.iCardIndex].Suits == j;
			Deck.iCardIndex--;
			cout << Deck.iCardIndex << endl;
		} 
		
	}
/*
	srand ( time(NULL) );

	for (int k = 0; k >= 0; k <= 51)
	{
		int r = ( rand() % 51 ) + 1;
		Deck.iCardIndex = r;
		cout << Deck.iCardIndex << endl;
	}
*/
	
	do
	{
		cout << "\nExit? (y/n): ";
		cin >> playAgain;
	} while (playAgain == 'n' );


	


	std::cin.sync();
	std::cin.get();
	return 0;
}
Should line 43 Deck.iCardIndex--;
be moved after line 44 cout << Deck.iCardIndex << endl;?
@Chervil - Ah! That makes sense!

Now I can't seem to figure out why then its displaying these numbers, its not also displaying the suit. Any ideas?
Okay, so I've got a working code so far. I need to now shuffle this deck using the pseudo-random number generator.

instructions:
For i = 51 down to 0
Let r = a random number between 0 and i (using rand () )
Swap the values of Deck.card[i] and Deck.card[r]

I'm just not sure how to swap the values for the deck.

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

using namespace std;

enum eSuit {C=0, D, S, H};

struct SOneCard
{
	int iRank;
	eSuit Suits;
};

struct SCardDeck
{
	SOneCard aiDeck[52];
	int iCardIndex;
	 
};

struct SPlayerHand
{
	SOneCard aiHand[13];
	int iTotalHand;
};

int main(int argc, char* argv[])
{
	char playAgain;

	SCardDeck Deck;
	Deck.iCardIndex = 0;

	//SOneCard rank[13] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
	//SOneCard suit;

	for(int i = 0; i < 4; i++)
	{
		for(int j = 0; j < 13; j++)
		{
			Deck.aiDeck[Deck.iCardIndex].iRank = j + 2;
			Deck.aiDeck[Deck.iCardIndex].Suits = eSuit(i);
			cout << Deck.iCardIndex << endl;
			Deck.iCardIndex++;
			
		}
	}

	srand( time( NULL ) );

	for( int i = 0; i <= 51; i++ )
	{
		int r = ( rand() % i ) + 1;
		
		
	}
	
	
	do
	{
		cout << "\nExit? (y/n): ";
		cin >> playAgain;
	} while (playAgain == 'n' );


	


	std::cin.sync();
	std::cin.get();
	return 0;
}
Anyone know why when I cout near the end of this code, I get just a line of about 6 or so random numbers and letters?

I could really use the help here!

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
77
78
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>

using namespace std;


enum eSuit {C=0, D, S, H};

struct SOneCard
{
	int iRank;
	eSuit Suits;
};

struct SCardDeck
{
	SOneCard aiDeck[52];
	int iCardIndex;
	 
};

struct SPlayerHand
{
	SOneCard aiHand[13];
	int iTotalHand;
};

int main(int argc, char* argv[])
{
	char playAgain;

	
	SCardDeck Deck;
	Deck.iCardIndex = 0;

	
	for(int i = 0; i < 4; i++)
	{
		for(int j = 0; j < 13; j++)
		{
			Deck.aiDeck[Deck.iCardIndex].iRank = j + 2;
			Deck.aiDeck[Deck.iCardIndex].Suits = eSuit(i);
			Deck.iCardIndex++;
			
		}
	}


	srand( time( NULL ) );

	for( int i = 0; i < 52; i++ )
	{
		int iRandomNumber = ( rand() % 51) + 1; 
		SOneCard card = Deck.aiDeck[i];
		Deck.aiDeck[i] = Deck.aiDeck[iRandomNumber];
		Deck.aiDeck[iRandomNumber] = card;
		
	}
	
	cout << Deck.aiDeck << endl;

	do
	{
		cout << "\nExit? (y/n): ";
		cin >> playAgain;
	} while (playAgain == 'n' );


	



	std::cin.sync();
	std::cin.get();
	return 0;
}
It's not random. It's the address of Deck.aiDeck which you ask cout to output near the end of your code:

cout << Deck.aiDeck << endl;

Ah, I see.

What would be the proper way to get the program to show me the shuffled cards?
Using a loop, as you do immediately above it, to display each individual element (or card) in the array (or deck.)
Working on the displaying of the rank and suit of the cards.

I have to use a function that takes a struct as a parameter. The function has to cout the card rank and suit. I should be using a switch statement for this.

Any ideas for at least setting up the function that takes a parameter of a struct?

These structs have been all confused. I'm not even sure about to set any of this up even with the switch statement.

Examples help!!

Current 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>

using namespace std;


enum eSuit {C=0, D, S, H};

struct SOneCard
{
	int iRank;
	eSuit Suits;
};

struct SCardDeck
{
	SOneCard aiDeck[52];
	int iCardIndex;
	 
};

struct SPlayerHand
{
	SOneCard aiHand[13];
	int iTotalHand;
};

struct SCardDisplay
{
	char cSuit;
	char cRank;
};

int main(int argc, char* argv[])
{
	char playAgain;

	
	SCardDeck Deck;
	Deck.iCardIndex = 0;

	
	for(int i = 0; i < 4; i++)
	{
		for(int j = 0; j < 13; j++)
		{
			Deck.aiDeck[Deck.iCardIndex].iRank = j + 2;
			Deck.aiDeck[Deck.iCardIndex].Suits = eSuit(i);
			Deck.iCardIndex++;
			
		}
	}


	srand( time( NULL ) );

	for( int i = 0; i < 52; i++ )
	{
		int iRandomNumber = ( rand() % 51) + 1; 
		SOneCard card = Deck.aiDeck[i];
		Deck.aiDeck[i] = Deck.aiDeck[iRandomNumber];
		Deck.aiDeck[iRandomNumber] = card;
		cout << Deck.aiDeck[i].iRank << " " << Deck.aiDeck[i].Suits << endl;
		
	}
	
	int iPlayersHand = 0;
	int iComputersHand = 0;

	void CardDisplay( SCardDisplay cCardSuit, SCardDisplay cCardRank )
	{

	}

	do
	{
		cout << "\nExit? (y/n): ";
		cin >> playAgain;
	} while (playAgain == 'n' );


	



	std::cin.sync();
	std::cin.get();
	return 0;
}
I have to use a function that takes a struct as a parameter. The function has to cout the card rank and suit. I should be using a switch statement for this.

Here's a skeleton of a possible approach:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void showCard(SOneCard);            // Declare function prototype

//-------------------------------------------

int main(int argc, char* argv[])
{

    for (int i=0; i< 52; ++i) {
        showCard(Deck.aiDeck[i]);    // Call the function
    }

    return 0;
}

//-------------------------------------------

void showCard(SOneCard card) {       // Define the function

    cout << card.iRank << " of ";
    cout << card.Suits << endl;

}


I temporarily removed the rest of the code so we can see the main features here.
Line 1, the function is declared. This is so the compiler knows what it returns (in this case nothing) and what type of parameter it takes (in this case the structure SOneCard).

At line 9 the function is called. Inside a loop here, but that's up to you. It could be called just once if that's what you need.

Line 17 is the full definition of the function. The parameter SOneCard is given the name "card", and that's how it will be used inside the function.
Lines 19 and 20 are just example code, you will most likely want to delete this and replace it with the switch/case to give the proper name and suit of the card.

This helps soooo much, Chervil!! Thank you so much!! :D
Now I need to draw the top card of the deck and ask the player whether he'd like to keep or discard it. If he keeps it, I have to add it to his hand. If he discards it, I have to put it back into the deck. If the player discards this first card, they pick up the next one.

I'm not sure on how to get the player to draw the top card of the shuffled deck.

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>

using namespace std;


enum eSuit {C=0, D, S, H};



struct SOneCard
{
	int iRank;
	eSuit Suits;
};

struct SCardDeck
{
	SOneCard aiDeck[52];
	int iCardIndex;
	 
};

struct SPlayerHand
{
	SOneCard aiHand[13];
	int iTotalHand;
};

struct SCardDisplay
{
	char cSuit;
	char cRank;
};

void showCard(SOneCard);
void drawACard();

int main(int argc, char* argv[])
{
	char playAgain;

	
	SCardDeck Deck;
	Deck.iCardIndex = 0;

	
	for(int i = 0; i < 4; i++)
	{
		for(int j = 0; j < 13; j++)
		{
			Deck.aiDeck[Deck.iCardIndex].iRank = j + 2;
			Deck.aiDeck[Deck.iCardIndex].Suits = eSuit(i);
			Deck.iCardIndex++;
			
		}
	}


	srand( time( NULL ) );

	for( int i = 0; i < 52; i++ )
	{
		int iRandomNumber = ( rand() % 51) + 1; 
		SOneCard card = Deck.aiDeck[i];
		Deck.aiDeck[i] = Deck.aiDeck[iRandomNumber];
		Deck.aiDeck[iRandomNumber] = card;
		//cout << Deck.aiDeck[i].iRank << " " << Deck.aiDeck[i].Suits << endl;
		showCard(Deck.aiDeck[i]);
		
	}
	
	int iPlayersHand = 0;
	int iComputersHand = 0;


	do
	{
		
		cout << "\nExit? (y/n): ";
		cin >> playAgain;
	} while (playAgain == 'n' );


	



	std::cin.sync();
	std::cin.get();
	return 0;
}

void showCard(SOneCard card)
{

	if( card.iRank >= 2 && card.iRank <=10 )
			cout << "You drew a " << card.iRank << " of ";
		else
			switch( card.iRank )
		{
			
			case 11:
				cout << "You drew a J" << " of ";
				break;
			case 12:
				cout << "You drew a Q"  << " of ";				
				break;
			case 13:
				cout << "You drew a K"  << " of ";
				break;
			case 14:
				cout << "You drew an A"  << " of ";
				break;
		}

	if( card.Suits >= 0 && card.Suits <= 4 )
			switch( card.Suits )
			{
				case 0:
					cout << 'C' << endl;
					break;
				case 1:
					cout << 'D' << endl;
					break;
				case 2:
					cout << 'S' << endl;
					break;
				case 3:
					cout << 'H' << endl;
					break;
			}

	
}

void drawACard()
{
	int r; // rank
	int s; // suit
	int n, card;

	
}
Any ideas?

Going crazy trying to figure this out, heh.
I've gotten to the point that it now displays a card, but now its doing all kinds of whacky things. It keeps displaying the same card for one thing.

I need it to display a card, if the players wants to keep it, it will be added to the player's hand. If not, it is put back into the deck. If the player does not want this first card, that card gets added back to the deck, and the second card will be added to the player's hand.

The computer must decide to keep the first card it draws and discard the second.

I'm so lost here and need help ASAP!



This is what I've got so far:

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <algorithm>

using namespace std;


enum eSuit {C=0, D, S, H};


struct SOneCard
{
	int iRank;
	eSuit Suits;
};

struct SCardDeck
{
	SOneCard aiDeck[52];
	int iCardIndex;
};

struct SPlayerHand
{
	SOneCard aiHand[13];
	int iTotalHand;
};

struct SCardDisplay
{
	char cSuit;
	char cRank;
};

void showCard(SOneCard);



int main(int argc, char* argv[])
{
	
	
	char cKeepCard;
	
	SCardDeck Deck; 


//	SCardDeck Deck;
	Deck.iCardIndex = 0;

	for(int i = 0; i < 4; i++)
	{
		for(int j = 0; j < 13; j++)
		{
			Deck.aiDeck[Deck.iCardIndex].iRank = j + 2;
			Deck.aiDeck[Deck.iCardIndex].Suits = eSuit(i);
			Deck.iCardIndex++;
			
		}
	}


	srand( time( NULL ) );

	for( int i = 0; i < 52; i++ )
	{
		int iRandomNumber = ( rand() % 51) + 1; 
		SOneCard card = Deck.aiDeck[i];
		Deck.aiDeck[i] = Deck.aiDeck[iRandomNumber];
		Deck.aiDeck[iRandomNumber] = card;
		//cout << Deck.aiDeck[i].iRank << " " << Deck.aiDeck[i].Suits << endl;
		//showCard(Deck.aiDeck[i]);
		
		
		
		
	}	

	do
	{
		SPlayerHand sPlayersHand;
		SPlayerHand sComputersHand;

		Deck.iCardIndex = 0;

		showCard(Deck.aiDeck[Deck.iCardIndex]);

		cout << "Keep this card? (y/n)";
		string sDecision;
		cin >> sDecision;

		if(sDecision == "y" || sDecision == "Y")
		{
			sPlayersHand.aiHand[0,1,2,3,4,5,6,7,8,9,10,11,12,13] = Deck.aiDeck[Deck.iCardIndex];;
			cout << "You discarded ";
			showCard(Deck.aiDeck[Deck.iCardIndex]);
			Deck.iCardIndex++;
		} else if (sDecision == "n" || sDecision == "N") 
		{
			sPlayersHand.aiHand[0,1,2,3,4,5,6,7,8,9,10,11,12,13] = Deck.aiDeck[Deck.iCardIndex];
			showCard(Deck.aiDeck[Deck.iCardIndex]);
			// add second card to hand instead of first
			//display second card that was added to hand
			Deck.iCardIndex++;
			showCard(Deck.aiDeck[Deck.iCardIndex]);
			Deck.iCardIndex++;
			sComputersHand.aiHand[0] = Deck.aiDeck[Deck.iCardIndex];
			Deck.iCardIndex++;

			// then draw one for computer
			// discard the next
		} else {
			cout << "That input is invalid. Try again." << endl;
		}
	}while (Deck.iCardIndex < 52);

	std::cin.sync();
	std::cin.get();
	return 0;
}

void showCard(SOneCard card)
{
	
		if( card.iRank >= 2 && card.iRank <=9 )
			cout << card.iRank;
				switch( card.iRank )
				{
					case 10:
						cout << "T";
						break;			
					case 11:
						cout << "J";				
						break;
					case 12:
						cout << "Q";				
						break;
					case 13:
						cout << "K";				
						break;
					case 14:
						cout << "A";				
						break;
				}

		if( card.Suits >= 0 && card.Suits <= 4 )
				switch( card.Suits )
				{
					case 0:
						cout << 'C' << endl;					
						break;
					case 1:
						cout << 'D' << endl;					
						break;
					case 2:
						cout << 'S' << endl;					
						break;
					case 3:
						cout << 'H' << endl;					
						break;
				}
	

		
	
	//	cout << card.iRank << " of ";
	//	cout << card.Suits << endl;
	
	


}
i was wondering if u can make an array that converts a decimal to binary. i have found result in the internet but its all complicated. im new to programming so i can't use the other codes that we haven't tacle yet
Do you pay attention to the warnings your compiler generates when you compile at all?

1>junk\main.cpp(65): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data

This one we can safely ignore.


1>junk\main.cpp(96): warning C4709: comma operator within array index expression
[1] This refers to:
sPlayersHand.aiHand[0,1,2,3,4,5,6,7,8,9,10,11,12,13] = Deck.aiDeck[Deck.iCardIndex];;
Which is equivalent to:
sPlayersHand.aiHand[13] = Deck.aiDeck[Deck.iCardIndex];


1>junk\main.cpp(102): warning C4709: comma operator within array index expression
[2] refers to:
sPlayersHand.aiHand[0,1,2,3,4,5,6,7,8,9,10,11,12,13] = Deck.aiDeck[Deck.iCardIndex];
which is equivalent to:
sPlayersHand.aiHand[13] = Deck.aiDeck[Deck.iCardIndex];


1>junk\main.cpp(41): warning C4100: 'argv' : unreferenced formal parameter
1>junk\main.cpp(41): warning C4100: 'argc' : unreferenced formal parameter

We can ignore those. But why bother with them if you aren't going to use them?


1>junk\main.cpp(45): warning C4101: 'cKeepCard' : unreferenced local variable

Your compiler has noticed you've defined a variable that is never used. Why is that?


1>main.cpp(96): warning C4789: buffer 'sPlayersHand' of size 108 bytes will be overrun; 8 bytes will be written starting at offset 104
1>junk\junk\main.cpp(102): warning C4789: buffer 'sPlayersHand' of size 108 bytes will be overrun; 8 bytes will be written starting at offset 104

Really? You ignored that? These two errors refer to [1] and [2] referenced above. You're attempting to write to index 13, which would be the 14th element, of sPlayersHand.aiHand. Unfortunately it only has 13 elements, so the highest index you could use there is 12.

Maybe you should pay attention to the warnings, eh?
Um, thanks?

Fixed, but didn't really help anything. :/

Thanks for your input! :)
Topic archived. No new replies allowed.