Colour Theme Font Size Options
 
 
 
Draw a Card

Draw a card - IOmanip practice


Video Summary: https://youtu.be/eN2Z3h7Y2Yk


In this exercise, you will generate a random number between 1 and 13, and place it in a "card" made of dashes and lines (they won't line up properly, but that is fine)

You may find these IOmanipulators helpful for this:

  • setw(int) - determines how much space the output should take up, and fills in the extra space as needed. Only affects the next item output
  • setfill('character') - changes the setw extra spaces to the character you put. Affects all future setws.
  • left/right - changes the alignment of the setw to left or right. Affects all future output.

Make sure when you draw a two-digit card the format doesn't break.

Suggested steps:

  1. Generate a top and bottom line of dashes using setw and setfill. For this to work, you will need to add "" at the end of the cout so there is something there.
  2. Generate some empty lines with border to add some spacing to the card at the top and bottom by changing setfill back to ' '
  3. Place the card value in the middle. Use setw to make sure the card doesn't break

A sample output might look like this:

------- 
|     |
|  8  |
|     |
-------

Solution