Review Session 5

Recording
The Card and Deck Example

Develop code to simulate a deck of playing cards.

Code start

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    int card = rand() % 52;
    switch (card % 13)
    {
    case 0: cout << "two"; break;
    case 1: cout << "three"; break;
    case 2: cout << "four"; break;
    case 3: cout << "five"; break;
    case 4: cout << "six"; break;
    case 5: cout << "seven"; break;
    case 6: cout << "eight"; break;
    case 7: cout << "nine"; break;
    case 8: cout << "ten"; break;
    case 9: cout << "jack"; break;
    case 10: cout << "queen"; break;
    case 11: cout << "king"; break;
    case 12: cout << "ace"; break;
    default:
        ;
    }
    cout << " of ";
    switch (card / 13)
    {
    case 0: cout << "clubs"; break;
    case 1: cout << "diamonds"; break;
    case 2: cout << "hearts"; break;
    case 3: cout << "spades"; break;
    default: ;
    }
    cout << endl;
}

Finish this example ...

Practice Exercise 5

Email only the source code for each practice exercise as an email attachment.  There is no due date.  Use "Practice Exercise 5" as the email subject.   Add (a) comment(s) at the top of your program with your name, practice exercise 5, your compiler and operating system.

Write a program in which you simulate a simply dice game.  The game consists of rolling two dice and adding the sum of the dice to a total.  The game continues until the total reaches 100 or more.  Your solution should include a roll function that displays the result of each roll and returns the sum.  If the sum of the two rolls is 7, then the function returns 0 and the total is reset to 0.  Your output should look similar to the following:

You rolled 6 and 5.  The total is 11
You rolled 4 and 1.  The total is 16
You rolled 1 and 5.  The total is 22
You rolled 3 and 5.  The total is 30
You rolled 2 and 2.  The total is 34
You rolled 5 and 4.  The total is 43
You rolled 3 and 2.  The total is 48
You rolled 4 and 1.  The total is 53
You rolled 1 and 6.  The total is 0
You rolled 6 and 4.  The total is 10
You rolled 5 and 6.  The total is 21
You rolled 1 and 3.  The total is 25
You rolled 2 and 4.  The total is 31
You rolled 3 and 4.  The total is 0
You rolled 2 and 6.  The total is 8
You rolled 3 and 1.  The total is 12
You rolled 1 and 2.  The total is 15
You rolled 2 and 2.  The total is 19
You rolled 5 and 1.  The total is 25
You rolled 2 and 1.  The total is 28
You rolled 5 and 1.  The total is 34
You rolled 1 and 4.  The total is 39
You rolled 4 and 2.  The total is 45
You rolled 1 and 2.  The total is 48
You rolled 2 and 6.  The total is 56
You rolled 1 and 2.  The total is 59
You rolled 4 and 2.  The total is 65
You rolled 5 and 3.  The total is 73
You rolled 6 and 3.  The total is 82
You rolled 2 and 4.  The total is 88
You rolled 3 and 2.  The total is 93
You rolled 2 and 1.  The total is 96
You rolled 4 and 4.  The total is 104