I am trying to make a deck of cards and minipulate them in 3 files part 1, part 2, and header. In the header I am trying to make the deck so that it would be easy to call up in both part 1 and part 2. In part 1 I am trying to call up all the parts I think would work in C and started on the main body. Calling all the variables and making sure it to get all the cards of the same suit together. I then am trying to shuffle the deck. After that I am trying to sorte the deck so that each of the cards is together with all 4 of their like cards. In part 2, I again called all the variables and put the deck in order. Only this time I made a bit array and zeroed it out. After that I had a loop pick a random number and put the card in that random place as long as the bit array was zero at that spot. However, this is not working at all and I would love any help on what I am doing wrong.

"deck.h" 11 lines, 221 characters 
#include <iostream>
int main()
{
for (int CurrentCard = 1; CurrentCard <= 52; CurrentCard++){
typedef enum { clubs, diamonds, hearts, spades} suit_t;
typedef struct
{
unsigned int number : 4;
suit_t suit : 2;
} card_t;
}


"part1.c" 51 lines, 937 characters 
#include <stdio.h>
#include <deck.h>
#include <iostream>
#include <stdlib.h>
main()
{
int b = 1;
int c = 1;
char shufflesave[1][];
deck deck[][];
for (int a = 1; a <= 52; a++){
if (a <= 13){
deck[a][].suit_t = 0;
deck[a][].card_t = a;
}
else if (a > 13 && a <= 26){
deck[a][].suit_t = 1;
deck[a][].card_t = a - 13;
}
else if (a > 26 && a <= 39){
deck[a][].suit_t = 2;
deck[a][].card_t = a-26;
}
else{
deck[a][].suit_t = 3;
deck[a][].card_t = a-39;
}
}
for (int shuffle = 0; shuffle <= 52; shuffle++){
int r = 1 + rand() % 51;
shufflesave[][] = deck[b][];
deck[r][] = deck[b][];
shufflesave[1][] = deck[r][];
b++;
}
cout << deck[][];
for (int sort = 0; sort <= 52; sort++){
for (int mult = 1; mult <= 4; mult++){
if (sort >= 1 && sort <= 52){
for (int mult = 1; mult <= 4; mult++){
if (deck[sort][] == sort + "*"){
shufflesave[1][] = deck[mult][];
deck[mult][] = deck[sort][];
deck[sort][] = shufflesave[1][];
}
}
}
}
}
cout << deck[];
}



"part2.c" 46 lines, 743 characters 
#include <stdio.h>
#include <deck.h>
int main()
{
int a;
int b;
int c;
b = 0;
bit CardBits[56][];
for (a = 0; a < 52; a++){
CardBits[a][] = 0;
}
int r;
char shuffleSave[1][];
deck deck[][];
int card;
for (card = 1; card <= 52; card++){
if (a <= 13){
deck[card][].suit_t  = 0;
deck[card][].card_t = a;
}
else if (a > 13 && a <= 26){
deck[card][].suit_t = 1;
deck[card][].card_t = a - 13;
}
else if (a < 26 && a <= 39){
deck[card][].suit_t = 2;
deck[card][].card_t = a - 26;
}
else{
deck[card][].suit_t = 3;
deck[card][].card_t = a - 39;
)
}
for (c = 0; c <= 52; c++){
r = 1 + rand() %51;
while (CardBits[c][] != 0){
c++;
}
shffleSave[][] = deck[c][];
deck[c][] = deck[r][];
deck[r][] = shuffleSave[][];
CardBits[c][] = 1;
}
cout << deck[][];
}

Recommended Answers

All 23 Replies

You're allowed one main function per program. Just one. You seem to be trying to put one in each file. This is very bad.

ok thanks will fix that. Any other reason it wont work? I keep getting the error message about improper ()'s

I noticed on lines 1 untill 12 that there is one curly brace too much.
Probably in other places too.
Look carefully at your indentation. It is not a subject to be overlooked.

ok thanks! However its still not working even with those two fixes.

2: iostream is a C++ header. Are you coding in C++? If you are, you can make all of this so much easier for yourself by dumping the idiomatic C and using C++ style instead.

9/10: Is that you specifying how much memory you want to use? Don't. It makes it harder to read and unless you're doing this for a wristwatch from the 80s, you've got all the memory you need and bitpacking like this will make it slower.

20: Apart from having more than one main, you've got a main returning an int, and this one here returning nothing, and then the one on line 73 returning an int again. If this is C++, main returns an int, always and forever, and nothing else is correct.

22/23: You're not being charged by the letter. Give your variables meaningful names, so that you know what they are and so that other people can read your code.

25: deck deck[][]; What is a deck object? You never defined a deck object, but here you're trying to create a 2D array of them, also named deck. This makes no sense at all. I gave up here.

Also wondering why you are putting your typedefs in a for loop.(lines(1-12))

I removed the iostream as it is in C not C++, the deck object is from the .h header file. I thought I had imported it correctly. As for why there is typedefs... well thats what they want me to use and is in the instructions to use. In fact they tell me in the instructions I have to somehow use lines 5 - 12 somehow to make the deck. I see what you guys are talking about and appologize that its so confusing I will be fixing it to make it less confusing.

the deck object is from the .h header file.

I don't see any type named deck being defined in the header files. I see a file named deck.h, but that's just a name. It's got nothing to do with what's inside the file.

If I was doing this, I would create a struct named card, to represent a card, and then I would create an array of 52 card structs named deck: card_t deck[52];

so here is the new code. I hope it is not as confusing. I am putting the original deck.h first and my own deck.h at the bottom. I would love your opinion on what version would be better. Thank you all for all your help so far its getting better though still not working properly.

// making the deck to be called.  Somehow this is supposed to work...
// thinking of taking the part 1 and part 2 making of the deck and putting it into here.
for (int CurrentCard = 1; CurrentCard <= 52; CurrentCard++){
typedef enum { clubs, diamonds, hearts, spades} suit_t;
typedef struct
{
unsigned int number : 4;
suit_t suit : 2;
} card_t;
}

#include <stdio.h>
#include <deck.h>
#include <stdlib.h>
main()
{
int b = 1; // this is going to corrispond to the first card latter.
char shufflesave[1][];
deck deck[][]; // deck as defined in deck.h that was imported.
for (int a = 1; a <= 52; a++){ // a is the individual cards in the deck of cards.
if (a <= 13){
deck[a][].suit_t = 0; // the suit in deck.h 0 should corrispond to the first suit in the .h file.
deck[a][].card_t = a; // a is used for each card 1 being ace, 2 - 10, 11 = jack, 12 = queen, 13 = king.
}
else if (a > 13 && a <= 26){
deck[a][].suit_t = 1; // the suit as defined in deck.h 1 is the second suit.
deck[a][].card_t = a - 13; // a used again for each card subtracting 13 each time for correct numbering.
}
else if (a > 26 && a <= 39){
deck[a][].suit_t = 2; // the third suit in deck.h
deck[a][].card_t = a-26; // a as the number subtracting 26 for the correct numbering.
}
else{
deck[a][].suit_t = 3; // the final suit in deck.h
deck[a][].card_t = a-39; // a as the number subtracting 39 for the correct numbering.
}
}
for (int shuffle = 0; shuffle <= 52; shuffle++){
int r = 1 + rand() % 51; // r is the random choice so that the deck will be correctly shuffled.
shufflesave[][] = deck[b][]; // holding the card that is going to be replaced.
deck[r][] = deck[b][]; // replacing the card in b with the randomly chosen card.
shufflesave[1][] = deck[r][]; // replacing the randomly chosen duplicate card with the saved card.
b++; // increasing b to go threw the entire deck of cards.
}
cout << deck[][];
for (int sort = 1; sort <= 13; sort++){ // starting the sorting of the cards.
for (int mult = 1; mult <= 4; mult++){ // making sure all the numbers are together as there are 4 of each number.
if (deck[sort][] != sort + "*"){ // making sure the right card is chosen.
shufflesave[1][] = deck[sort][]; // saving the card to be replaced.
b = sort; // now b will be the number that we are looking for.
while (deck[b][] != sort + "*")
b++; // this loop should look for the next card of the same number until found.
deck[sort][] = deck[b][]; // replacing the card that is not right with the card found.
deck[b][] = shufflesave[1][]; // replacing the now duplicate card with the one replaced.
}
}
}
cout << deck[][];
}

#include <stdio.h>
#include <deck.h>
int main()
{
int a; // used as a way to clear the bit array and zero it out.
int card; // used for each card in making the deck.
int c; // used for each card in the deck latter.
int r; // the random number used latter.
bit CardBits[56][];
for (a = 0; a < 52; a++){
CardBits[a][] = 0; // zeroing out the bit array.
}
char shuffleSave[1][];
deck deck[][]; // making a deck object imported from deck.h.
int card;
for (card = 1; card <= 52; card++){
if (card <= 13){
deck[card][].suit_t  = 0; // first suit as defined in deck.h.
deck[card][].card_t = a; // cards 1 = ace, 2 - 10, 11 = jack, 12 = queen, 13 = king.
}
else if (card > 13 && card <= 26){
deck[card][].suit_t = 1; // deck.h second suit.
deck[card][].card_t = a - 13; // cards subtracting 13 for correct numbering.
}
else if (card < 26 && card <= 39){
deck[card][].suit_t = 2; // deck.h third suit.
deck[card][].card_t = a - 26; // cards subtracting 26 for correct numbering.
}
else{
deck[card][].suit_t = 3; // final suit in deck.h
deck[card][].card_t = a - 39; // cards subtracting 39 for correct numbering.
}
}
for (c = 0; c <= 52; c++){
r = 1 + rand() %51; // generating the random number.
while (CardBits[c][] != 0){
c++; // if the next bit in the bit array is already 0 going to the next bit in the bit array.
}
shffleSave[][] = deck[c][]; // saving the card to be replaced.
deck[c][] = deck[r][]; // replacing the card with the randomly chosen card.
deck[r][] = shuffleSave[][]; // replacing the now duplicate card with the saved card.
CardBits[c][] = 1; // making that bit 1 so it is skiped in the next loop.
}
cout << deck[][];
}

alternate version of deck.h that may work better

// making the deck to be called.  Somehow this is supposed to work...
char suit_t[]; // defining suit_t and card_t not sure if this is right but feels right.
char card_t[];
for (int CurrentCard = 1; CurrentCard <= 52; CurrentCard++){
typedef enum { clubs, diamonds, hearts, spades} suit_t;
typedef struct
{
unsigned int number : 4;
suit_t suit : 2;
} card_t;
}
typedef deck[number][CurrentCard]; // making deck a structure of typedef.  This is probabaly wrong.
int card;
for (card = 1; card <= 52; card++){
if (card <= 13){
suit_t = "clubs";
card_t = card;
deck[number-3][card] = suit_t + card_t;
}
else if (card > 13 && card <= 26){
suit_t = "diamonds";
card_t = a - 13; // cards subtracting 13 for correct numbering.
deck[number-2][card] = suit_t + card_t;
}
else if (card < 26 && card <= 39){
suit_t = "hearts";
card_t = a - 26; // cards subtracting 26 for correct numbering.
deck[number-1][card] = suit_t + card_t;
}
else{
suit_t = "spades";
card_t = a - 39; // cards subtracting 39 for correct numbering.
deck[number][card] = suit_t + card_t;
}
}

You're still going wrong creating the deck of cards.

Think about what you're trying to do. You're trying to create 52 card items. Is there any need to create a 2D array? Not that I can see.

So just make an array of 52 card items.

card_t deck[52];
That's the deck. That's all there is to it.

typedef deck[number][CurrentCard]; That's.... well, nonsensical.

My life would look sunnier if you would please remove that for loop on line 4 around your typedefs. Pleeeease. :o)

ok thank you both I am trully thankful for all your help

so would this be right for the deck.h then?

// making the deck to be called.  Somehow this is supposed to work...
char suit_t[]; // defining suit_t and card_t not sure if this is right but feels right.
char card_t[];
int card_n; // card number used for the deck of cards
typedef enum { clubs, diamonds, hearts, spades} suit_t;
typedef struct
{
unsigned int number : 4;
suit_t suit : 2;
} card_t;
card_t deck[52]; // making the deck.
int card;
for (card = 1; card <= 52; card++){
if (card <= 13){
suit_t = "clubs";
card_n = card;
deck[card] = suit_t + card_n;
}
else if (card > 13 && card <= 26){
suit_t = "diamonds";
card_n = a - 13; // cards subtracting 13 for correct numbering.
deck[card] = suit_t + card_n;
}
else if (card < 26 && card <= 39){
suit_t = "hearts";
card_n = a - 26; // cards subtracting 26 for correct numbering.
deck[card] = suit_t + card_n;
}
else{
suit_t = "spades";
card_n = a - 39; // cards subtracting 39 for correct numbering.
deck[card] = suit_t + card_n;
}
}

My deck.h would not exist.

I would call it card.h

It would look like this:

typedef enum { clubs, diamonds, hearts, spades} suit_t;

typedef struct card {
   int number;
   suit_t suit;
} card_t;

It would define a card type.

That's it. That's all it needs. All that junk in your header where you're setting values has no business being in your header.

Then:

#include "card.h"

int main()
{
  // create a deck of 52 cards

  card_t deck[52];

  // set cards to starting values
  for (int i = 0; i<13; ++i)
  {
    deck[i].number = i;
    deck[i].suit = clubs;
  }

  for (int i = 13; i<26; ++i)
  {
    deck[i].number = i - 13;
    deck[i].suit = hearts;

  }

  // and so on

ok I see I see... just confusing because all that other stuff I was told I had to include so it just frustrating to know that it was useless.

Still gitting badly placed ()'s

// making the cards. now in cards.h
typedef enum { clubs, diamonds, hearts, spades} suit_t;
typedef struct
{
int number;
suit_t suit;
} card_t;

// part 1 to be run seperate from part 2 hence two mains.
#include <stdio.h>
#include <deck.h>
#include <stdlib.h>
main()
{
int b = 1; // this is going to corrispond to the first card latter.
char shufflesave[1];
deck deck[52]; // deck as defined in deck.h that was imported.
int card;
for (card = 1; card <= 52; card++){
if (card <= 13){
deck[card].number = card;
deck[card].suit = clubs;
}
else if (card > 13 && card <= 26){
deck[card].number = card - 13;
deck[card].suit = diamonds;
}
else if (card < 26 && card <= 39){
deck[card].number = card - 26;
deck[card].suit = hearts;
}
else{
deck[card].number = card - 39;
deck[card].suit = spades;
}
}
for (int shuffle = 0; shuffle <= 52; shuffle++){
int r = 1 + rand() % 51; // r is the random choice so that the deck will be correctly shuffled.
shufflesave[] = deck[b]; // holding the card that is going to be replaced.
deck[r] = deck[b]; // replacing the card in b with the randomly chosen card.
shufflesave[1] = deck[r]; // replacing the randomly chosen duplicate card with the saved card.
b++; // increasing b to go threw the entire deck of cards.
}
cout << deck[];
for (int sort = 1; sort <= 13; sort++){ // starting the sorting of the cards.
for (int mult = 1; mult <= 4; mult++){ // making sure all the numbers are together as there are 4 of each number.
if (deck[sort] != sort + "*"){ // making sure the right card is chosen.
shufflesave[1] = deck[sort]; // saving the card to be replaced.
b = sort; // now b will be the number that we are looking for.
while (deck[b] != sort + "*")
b++; // this loop should look for the next card of the same number until found.
deck[sort] = deck[b]; // replacing the card that is not right with the card found.
deck[b] = shufflesave[1]; // replacing the now duplicate card with the one replaced.
}
}
}
cout << deck[];
}

#include <stdio.h>
#include <deck.h>
int main()
{
int a; // used as a way to clear the bit array and zero it out.
int card; // used for each card in making the deck.
int c; // used for each card in the deck latter.
int r; // the random number used latter.
card_t deck[52]; // making the deck
bit CardBits[56];
for (card = 1; card <= 52; card++){
if (card <= 13){
deck[card].number = card;
deck[card].suit = clubs;
}
else if (card > 13 && card <= 26){
deck[card].number = card - 13;
deck[card].suit = diamonds;
}
else if (card < 26 && card <= 39){
deck[card].number = card - 26;
deck[card].suit = hearts;
}
else{
deck[card].number = card - 39;
deck[card].suit = spades;
}
}
for (a = 0; a < 52; a++){
CardBits[a] = 0; // zeroing out the bit array.
}
char shuffleSave[1];
for (c = 0; c <= 52; c++){
r = 1 + rand() %51; // generating the random number.
while (CardBits[c] != 0){
c++; // if the next bit in the bit array is already 0 going to the next bit in the bit array.
}
shffleSave[] = deck[c]; // saving the card to be replaced.
deck[c] = deck[r]; // replacing the card with the randomly chosen card.
deck[r] = shuffleSave[]; // replacing the now duplicate card with the saved card.
CardBits[c] = 1; // making that bit 1 so it is skiped in the next loop.
}
cout << deck[];
}

edit to above deck deck[52] is now card_t deck[52]; still not working though

Line 1: FYI you are NOT making the cards in deck.h, You are just defining the types that compose a card.
When the compiler executes card_t deck[52] in your main function, it is still not making the cards. It is just reserving space to hold 52 (0..51) places of type card_t.
It is called an array.
Now put in the Init code by Moshops to fill this array with values.

 // set cards to starting values
for (int i = 0; i<13; ++i)
{
deck[i].number = i;
deck[i].suit = clubs;
}
for (int i = 13; i<26; ++i) . . . etc.

If that is done, you will have a brand new deck of cards!
I suggest now, you to write a function to display a card, so you can show of with your deck of cards. I would also change "number" in the typedef of t_card into the more meaningful "cardnr" or something like that.

Here is another excellent, excellent tip.

When you are programming, do not wait until you have written the entire programme before building and testing. As a beginner, you should be building and testing your code roughly every time you add a line. Maybe every two or three lines.

commented: Every line! +15

ok thank you both so much for your time. I guess I am still confused as I did this in one loop instead of multiple for loops.

for (card = 1; card <= 52; card++){
if (card <= 13){
deck[card].number = card;
deck[card].suit = clubs;
}
else if (card > 13 && card <= 26){
deck[card].number = card - 13;
deck[card].suit = diamonds;
}
else if (card < 26 && card <= 39){
deck[card].number = card - 26;
deck[card].suit = hearts;
}
else{
deck[card].number = card - 39;
deck[card].suit = spades;
}
}

yet it is still saying that the ()'s are bad. So do I just need to make a entirly seprate loop for each suit? Why would this make the program give me back the error bad ()'s for something like this?

I am sorry I sound so newbe I just do not get this one bit and would love to understand.

Line 10 should read else if (card > 26 && card <= 39){ instead of else if (card < 26 && card <= 39){

Why did you not follow the sugestion of Moschops?
As you can see one for loop with a complicated if else if can be error prone.

just easyer to leave it as is.... I just got it to the point in that it compiles. Thank You both for all your help. However, when I run the code I still get the error badly placed ()'s with other changes.... whats going on?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.