/*  The D&D Dice checker example
    Marc Chee, February-June 2019
    marc.chee@unsw.edu.au

    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 Tuesday 04 June 2019, 01:44:09 AM, last modified Wednesday 05 June 2019, 11:17:07 PM.

file: diceCheck.c


Back to top

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