/*  The D&D Dice checker example
    Marc Chee, February 2019

    This small example will ask the user to input the
    result of two dice rolls.
    It will then check the sum of them against its 
    secret number.
    It will report back: 
        a success (higher)
        a failure (lower)
        a tie (equal)
*/

#include <stdio.h>

// the secret and unchangeable target number
// note the all caps name that reminds us
// that it's a constant
#define SECRET_TARGET 7

int main (void) {
    // set up some dice variables so we can store numbers
    int dieOne;
    int dieTwo;
    int total;
    
    // we start by asking the user for their dice rolls
    printf("Please enter your first die roll: ");
    // then scan their input
    scanf("%d", &dieOne);
    // repeat for the second die
    printf("Please enter your second die roll: ");
    scanf("%d", &dieTwo);
    
    // calculate the total and report it
    total = dieOne + dieTwo;
    printf("Your total roll is: %d\n", total);
    
    // Now test against the secret number
    if (total > SECRET_TARGET) {
        // success
        printf("Skill roll succeeded!\n");
    } else if (total == SECRET_TARGET) {
        // tie
        printf("Skill roll tied!\n");
    } else {
        // the same as total < SECRET TARGET
        // but we don't have to test it because
        // we've already checked all other 
        // possibilities
        printf("Skill roll failed!\n");
    }
}

Resource created Wednesday 20 February 2019, 10:48:22 PM, last modified Sunday 24 February 2019, 08:50:05 PM.

file: diceCheck.c


Back to top

COMP1511 19T1 (Programming Fundamentals) is powered by WebCMS3
CRICOS Provider No. 00098G