[1st year University Comp Sci] Creating a deck of cards using Structs

So I've been a little pressed for time the past couple days and have this somewhat large assignment due in 2 days. But I can't even figure out how to get started! I've been trying to do part F of the project which is "A function that initializes a given deck array with the 52 standard playing cards". We are NOT allowed to use classes for this project and i've been struggling with structs so this is what I have so far:

card.h:
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
 #ifndef CARD_H
#define CARD_H


#include <iostream>
#include <cassert>
#include <fstream>
#include <string>

using namespace std;

const unsigned int DECK_SIZE = 52;
const unsigned int MAX_RANK = 13;
const unsigned int MAX_SUIT = 4;

const string ranks[MAX_RANK] =
{
	"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
	"Nine", "Ten", "Jack", "Queen", "King"
};
const string suits[MAX_SUIT] =
{
	"Spades", "Hearts", "Diamonds", "Clubs"
};

struct Deck
{
	string ranks;
	string suits;
};

Deck initializeDeck();


card.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "card.h"

Deck initializeDeck()
{
	int i, j;
	int k = 0;
	struct Deck card[DECK_SIZE];
	

	for (i = 0; i < MAX_RANK; i++)
	{
		for (j = 0; i < MAX_SUIT; i++)
		{
			card[k].ranks = ranks[i];
			card[k].suits = suits[j];
			k++;
		}
	}
}


main.cpp
1
2
3
4
5
6
7
8
9
#include "card.h"

int main()
{
	cout << "Now running card.cpp..." << endl;
	initializeDeck();

	return 0;
}


I have no idea what I've actually done here as I was just following a bunch of different examples. Compiling and running this just results in a segmentation error and I've been stuck and frustrated for the past four hours now.



P.S. if anyone is curious, here are the other tasks of the project:
a. a record type (i.e., struct) for modeling a card
b. a function that returns the string representation of a given card
c. a function that compares if one card is more valuable than another card
Ranks are ordered, from higher to lower, as follows: ace, king, queen, jack, 10, 9, ..., 2. We follow the convention of Contract Bridge, and consider Spades the most valuable, followed by Hearts, and then Diamonds, and lastly Clubs.
d. a function that tests if two given cards are the same in suit and in rank
e. an array type, in the form of a typedef, for representing a deck of cards
f. a function that initializes a given deck array with the 52 standard playing cards
g. a function that randomly shuffles a given deck of cards
h. a function that prints a given deck of playing cards to an output file stream
i. a function that reads a given deck of playing cards from an input file stream
j. Develop a C++ program, shuffle, that prints a randomly shuffled deck of playing cards to file output. The output of the program shuffle should follow the format specified in part h of the previous question.
k. Develop a C++ filter, game, that reads from file input a deck of playing cards (assumed to have been generated by the program shuffle) and simulates the following game using the input deck. *Picture of example game*
In C++, a struct and a class are the same thing. They differ in default visibility and inheritance, but they're the same thing. Looks like you're being taught C++ by someone who actually speaks C but has heard of cout. This is bad news, but it's what you're stuck with, I suppose.

Your function Deck initializeDeck() claims to return an object of type Deck. It actually returns nothing.

1
2
3
4
5
struct Deck
{
	string ranks;
	string suits;
};

Looks like this is actually a single card. Why is this struct named Deck? That will be massively hampering your thinking every time you look at your code.

You create an array inside your function initializeDeck(). That array will cease to exist when that function ends. This is a bad thing.

I think you should have done a to e first. You've jumped to f, but it looks like a clear progression intended to guide you through this.
Last edited on
Your Deck struct should be called Card since a deck is a collection of cards.
Then you create a variable called Deck.
To initialize it you can pass it to your function.
1
2
3
4
5
Card deck[DECK_SIZE];
void initializeDeck(Card deck[])
{
   // initialize all cards 
} 
Topic archived. No new replies allowed.