• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C help - Involves printf

deadlyapp

Diamond Member
Alright, so currently working on an assignment that has me making a deck of cards, shuffling those cards, and manipulating it in a couple of ways. I've finished all that part but I also need to build a function that displays the remaining cards in the deck. There is also a parameter passed to this function on how many cards to display per row.

Here's the information:
Code:
/* Displays the name and suit of all undelt cards in a deck to the console using
 * printf (see samples below). The cards are displayed in the order they occur
 * in the deck (lowest undelt index first). The specified number of cards are
 * shown on each line, if possible. The cards are separated by a comma-space
 * but the last card in a row does not have a comma-space. In addition, there is
 * a newline, '\n', printed after the last row. If there are no undelt cards in
 * the deck the method prints "Empty Deck" with a '\n'. Note that there are no
 * spaces or characters of any kind displayed before the first card in a row or
 * the Empty Deck message.
 *
 * Sample Output (52 undelt cards, 13 specified for cardsPerRow):
 *
 *    See comment for newDeck() above.
 * 
 * Sample Output (9 undelt cards in the deck, 7 specified for cardsPerRow):
 *
 *    4D, KH, 9C, 2C, 3D, QS, 3S
 *    5H, XD
 *
 * Sample Output (5 undelt cards in the deck, 9 specified for cardsPerRow):
 *
 *    7S, KD, JH, 2H, 8C
 *
 * Sample Output (zero undelt cards in deck, N specified for cardsPerRow):
 *
 *    Empty Deck 
 *
 * param cardsPerRow: The number of cards to show per row.  
 */
void displayDeck(int cardsPerRow);

my current code looks like this:
Code:
void displayDeck(int cardsPerRow)
{
   int i;

   if (hasNextCard)
   {
      for (i = 0; hasNextCard(); i++)
      {
      printf("%c%c, ", deck[i].name, deck[i].suit);
      }
   }
   else
   {
      printf("Empty Deck\n");
   }
}

where the hasNextCard function tells me whether or not I have another card. If I don't it exits with "empty deck".

I simply can't think of a way to be able to manipulate how many I print in each row though. I'm sure it's something trivial.

Thanks for any help that anyone can give me.
 
Also as you can see this code i've written will print each index of the card array one after the other until it gets to the end of the deck.
 
You mean something like this after the printf? Just a new line after so many cards?

Code:
if( i%cardsPerRow == 0)printf("\n");
 
Rofl who knew it was as easy as that. I'll try it later this afternoon to see how it works.

I think i'll want it as a for loop though. I guess I can just nest it in a different way.
 
Thanks for the help. That put me in the right direction, I just had to modify the rest of my function to match the professor's output.
 
Back
Top