Deck of Cards in C++

I am trying to create a card game in C++ that every turn a user will drop a card from their hand or add a card to their hand depending on the input by the user. I need to create a deck of cards and for some reason what I have now is giving me a segmentation fault or floating point exception core dump when I try to print the deck to see if it is working. Any ideas how to fix this? I declared int i before these for loops that is not the error.
Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for(int j=0; j<4;j++)
{
   for(i=1; i<13;i++)
   {
	C.rank = i;
	C.suit='S';
	availDeck.AddCard(C);
	C.suit = 'H';
	availDeck.AddCard(C);
	C.suit='D';
	availDeck.AddCard(C);
	C.suit='C';
	availDeck.AddCard(C);
    }
}

Last edited on
To start:
What is the type of C?
What is the type of availDeck?
What does AddCard do?

You've given very little context, but it seems likely the offending code is somewhere in the definition of the type of availDeck.
C is a card type, card is a class that holds an int rank and a char suit. Availdeck is of Deck type, deck is also a class that holds number of cards and a cards array of 52 cards (the array is also of card class type).

Here is the AddCard function:
1
2
3
4
void Deck::AddCard(Card newcard)
	{
		cards[n_cards++]=newcard;
	}


I'm sorry I didn't mean to be vague I thought my issue lied in my for loop alone. Thanks!
Last edited on
You're calling AddCard() 208 times. You haven't shown the declaration of Deck, but you stated above that it holds 52 cards. You're going to get an out of bounds reference when you try to add the 53rd card.

You have nested loops, but you don't in fact need the outer loop.

1
2
3
4
5
6
7
8
9
10
11
  for(i=1; i<13;i++)
  {  C.rank = i;
    C.suit='S';
    availDeck.AddCard(C);
    C.suit = 'H';
    availDeck.AddCard(C);
    C.suit='D';
    availDeck.AddCard(C);
    C.suit='C';
    availDeck.AddCard(C);
  }


PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
what should I do if I am getting the same error when I only use the single loop? Thanks so much for your help!
This is how I am trying to test if my deck is being filled, maybe I am printing it out incorrectly?:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Card C;
 int i=0;

 for(i=1; i<13;i++)
 {
	C.nrank = i;
	C.suit='S';
	availDeck.AddCard(C);
	C.suit='H';
        availDeck.AddCard(C);
	C.suit='D';
	availDeck.AddCard(C);
	C.suit='C';
	availDeck.AddCard(C);
  }
cout<<availDeck.cards[i].nrank;




Last edited on
I think the idea with two loops is correct since you have 4 suits and 13 values.
Try 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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

const int NUM_SUITS = 4;
const int NUM_VALUES = 13;

struct Card
{
  int suit;
  int value;
};

int main()
{
  vector<Card> deck;

  for (int suit = 0; suit < NUM_SUITS; suit++)
    for (int value = 0; value < NUM_VALUES; value++)
    {
      Card card;
      card.suit = suit;
      card.value = value;
      deck.push_back(card);
    }
  for (Card card : deck)
  {
    cout << "Suit: " << card.suit << "\tValue: " << card.value << "\n";
  }
  system("pause");
  return 0;
}

OUTPUT

Suit: 0 Value: 0
Suit: 0 Value: 1
Suit: 0 Value: 2
Suit: 0 Value: 3
Suit: 0 Value: 4
Suit: 0 Value: 5
Suit: 0 Value: 6
Suit: 0 Value: 7
Suit: 0 Value: 8
Suit: 0 Value: 9
Suit: 0 Value: 10
Suit: 0 Value: 11
Suit: 0 Value: 12
Suit: 1 Value: 0
Suit: 1 Value: 1
Suit: 1 Value: 2
Suit: 1 Value: 3
Suit: 1 Value: 4
Suit: 1 Value: 5
Suit: 1 Value: 6
Suit: 1 Value: 7
Suit: 1 Value: 8
Suit: 1 Value: 9
Suit: 1 Value: 10
Suit: 1 Value: 11
Suit: 1 Value: 12
Suit: 2 Value: 0
Suit: 2 Value: 1
Suit: 2 Value: 2
Suit: 2 Value: 3
Suit: 2 Value: 4
Suit: 2 Value: 5
Suit: 2 Value: 6
Suit: 2 Value: 7
Suit: 2 Value: 8
Suit: 2 Value: 9
Suit: 2 Value: 10
Suit: 2 Value: 11
Suit: 2 Value: 12
Suit: 3 Value: 0
Suit: 3 Value: 1
Suit: 3 Value: 2
Suit: 3 Value: 3
Suit: 3 Value: 4
Suit: 3 Value: 5
Suit: 3 Value: 6
Suit: 3 Value: 7
Suit: 3 Value: 8
Suit: 3 Value: 9
Suit: 3 Value: 10
Suit: 3 Value: 11
Suit: 3 Value: 12
what should I do if I am getting the same error when I only use the single loop?

Step through your code in a debugger. This will allow you to see exactly where your code is crashing, and examine the contents of the memory at that point to see why it's crashing.
What debugger should I use? The ones Ive found online all give me the output as my console without any further explanation.. thanks so much for all your help!
What compiler / IDE does you use?
Code::Blocks
Topic archived. No new replies allowed.