Return To CS 110 Home

Colour Theme   Font Size Options
 
   
   
   
Switch Practice

Switch Practice


Video Summary: https://youtu.be/K6sqnxdK-b4


Consider this program that rolls a random number between 1 and 6:

 
#include <iostream>
#include <time.h>
using namespace std;

int main(){
    srand(time(NULL));
    int dice = rand() % 6 + 1;

    cout << "The dice roll is: " << dice << endl;

}

Using a switch statement, evaluate the available rolls as follows:

  1. If the case is 1 or 2, tell the player it was a bad roll
  2. If the case is 3, tell the player it was a medium roll
  3. If the case is 4 or 5, tell the player it was a good roll
  4. If the case is 6, tell the player it was the best roll

Since 1-2 and 4-5 have the same message, you should only need one cout for both options, with a total of 4, if you do your switch correctly. When you are finished, your output might look like this:

The dice roll is: 4
A good roll

Solution