< Previous | Contents | Next >

Telling the Story

Next I use the variables to tell the story.

//tell the story

cout << "\nA brave group of " << adventurers << " set out on a quest "; cout << "-– in search of the lost treasure of the Ancient Dwarves. "; cout << "The group was led by that legendary rogue, " << leader << ".\n";


cout << "\nAlong the way, a band of marauding ogres ambushed the party. "; cout << "All fought bravely under the command of " << leader;

cout << ", and the ogres were defeated, but at a cost. ";

cout << "Of the adventurers, " << killed << " were vanquished, "; cout << "leaving just " << survivors << " in the group.\n";

Summary 35

cout << "\nThe party was about to give up all hope. "; cout << "But while laying the deceased to rest, "; cout << "they stumbled upon the buried fortune. ";

cout << "So the adventurers split " << GOLD_PIECES << " gold pieces."; cout << leader << " held on to the extra " << (GOLD_PIECES % survivors); cout << " pieces to keep things fair of course.\n";

return 0;

}

The code and thrilling narrative are pretty clear. I will point out one thing, though. To calculate the number of gold pieces that the leader keeps, I use the modulus operator in the expression GOLD_PIECES % survivors. The expression evaluates to the remainder of GOLD_PIECES / survivors, which is the number of gold pieces that would be left after evenly dividing the stash among all of the surviving adventurers.