Return To CS 110 Home

Colour Theme   Font Size Options
 
   
   
   
Function Practice

Function practice - dividing dice roll game into functions


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


Consider this solution to a previous exercise, which provided a looping game of rolling dice:


The goal of this exercise is to divide the functionality up into the following functions without breaking the game:

  1. int rollDice() - Generates and returns a single random number
  2. void checkResult(int dice1, int dice2) - Takes two int input, evaluates which is larger, and prints the winner (or draw)
  3. void userPrompt(string& choice) - Takes the reference of a string choice. Prompts the user to play again. If the user presses y, the value of choice is changed to 'y', if the user presses n, the value is changed to 'n', and if the input is not valid the user is prompted again

In general, this exercise can mostly be completed by copy and pasting the existing code into the appropriate functions, and then adding appropriate function calls to the main program. The output should remain the same as what you start with.

Solution