0

I am making a small ANSI C application using GCC in Ubuntu which uses strcpy() and sorting.

My header:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define DECKSZ 52

typedef struct card {
    enum {ACE=1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING} pips;
    enum {SPADES, CLUBS, HEARTS, DIAMONDS} suit;
    char cardName[20];
} card;

card deck[DECKSZ];

void initDeck(card[]);
void labelCards();
void shuffleDeck(card[]);
void swap(card*,card*);

My main file:

#include "CardOps.h"

void initDeck(card deck[]) {
    int counter;
    for (counter = 0; counter < DECKSZ; counter++) {
        deck[counter].pips = (const)((counter % 13) + 1);
        deck[counter].suit = (const)(counter / 13);
    }
}

void labelCards() {
    char pips[13][6] = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
    char suits[4][9] = {"Spades","Hearts","Diamonds","Clubs"};
    int i;
    card cardName;
    for (i = 0; i < DECKSZ; i++) {
        strcpy(cardName, pips[i]);
        strcpy(cardName, suits[i]);
    }
}

int displayCards(int numCards) {
    int i, countCards;
    if (numCards > 52)
        countCards = 52;
    else
        countCards = numCards;
    for (i = 0; i < countCards; i++) {
        printf(cardName);
    }
    return countCards;
}

void shuffleDeck(card deck[]) {
    int i, j;
    for (i = 0; i < DECKSZ; i++) {
        j = rand() % DECKSZ;
        swap(&deck[i], &deck[j]);
    }
}

void SortCards() {

}

void swap(card *c1, card *c2) {
    card temp;
    temp = *c1;
    *c1 = *c2;
    *c2 = temp;
}

int main(void) {
    initDeck(deck);
    shuffleDeck(deck);
    return EXIT_SUCCESS;
}

I am trying to implement the following functionality:

A function called LabelCards() that takes as an argument a void and returns a void. Use each of the two enumeration type members in the deck to assign a string to cardName, i.e. “Queen of Hearts”. (You’ll want to create an array of strings “Ace”, “Two”, “Three”, etc. for the pips and a similar array for the suits to handle the string processing.) Note that you’ll need to use strcpy() to make the actual assignment to cardName.

A function called DisplayCards() that takes as an argument an integer N, and returns an integer. The function should display the cardName of first the N cards in the deck. The function returns the number of cards displayed, which could be less than the actual deck size. For example, 53 cards cannot be displayed in a deck of 52; I need to check for this and return the actual number of cards displayed.

A function called SortDeck() that takes and returns a void. My function should use the swap() algorithm to organize the cards in the deck first by their pips, and then according to their suit value.

Would somebody please help me get the functionality working? Thanks!

Benjamin
  • 345
  • 2
  • 3
  • 15
  • This question appears to be off-topic because it not one specific question that is likely to help future visitors. (There are at least two issues here: sorting and strcpy.) Asking two specific questions about how to sort and how to use strcpy might be more appropriate. – Adrian McCarthy Mar 21 '14 at 15:56
  • Okay I'll split it into two questions. Thanks. – Benjamin Mar 21 '14 at 19:32

1 Answers1

0

Don't put the declaration of deck in the header. You should make it an extern, and move the declaration into the main file.

Your initDeck function is indexing the array with DECKSZ which is constant and out of bounds. You probably meant to use counter instead.

labelCards is trying to copy the entire array into a single card. The types don't match, and the loop variable isn't used.

displayCards prints cardName, which doesn't exist. Presumably you meant to print the cardName of a specific card.

pat
  • 12,587
  • 1
  • 23
  • 52
  • `displayCards` is supposed to print the `cardName` of a specific card, yes. How would I use the loop variable inside `labelCards`? – Benjamin Mar 21 '14 at 15:39
  • Where are the cards you want to label in `labelCards`? Do you really need a separate function to label the cards, or can't you label each card as you initialize them in `initDeck`? Also, for a local array of strings, you should use `static const char *pipNames[] = { "Ace", ... };`, and don't forget that the array is indexed from 0, but your ace has a value of 1. – pat Mar 21 '14 at 15:50
  • In `displayCards`, where are the cards you want to display? If you pass the deck as an argument, you can index it with `i`, and print the `cardName` of *that* card. – pat Mar 21 '14 at 15:52
  • Also, `strcpy` will overwrite the target string, so the suit will overwrite the pips in the card. You could use `strcpy` for the pips, and `strcat` for the suit, but that would leave you with `"AceSpades"` for instance. Maybe `sprintf("%s of %s", pipNames[pips-1], suitNames[suit])` would be better (or `snprintf` so you don't overflow) – pat Mar 21 '14 at 15:59
  • Hmm, it looks like this is an assignment with some silly rules. I guess you have to assume there is a single global deck, and you have to use strcpy. Good luck with that! – pat Mar 21 '14 at 16:02