Rolls two dice and presents the total and then asks the user to guess the next total - C Data Type

C examples for Data Type:Random Number

Description

Rolls two dice and presents the total and then asks the user to guess the next total

Demo Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>

int main()//from   w  w  w .  ja  v a  2s .  co m
{
    int dice1, dice2;
    int total1, total2;
    time_t t;
    char ans;

    srand(time(&t));

    // give you a number between 0 and 5, so the + 1 makes it 1 to 6

    dice1 = (rand() % 5) + 1;
    dice2 = (rand() % 5) + 1;
    total1 = dice1 + dice2;

    printf("First roll of the dice was %d and %d, ", dice1, dice2);
    printf("for a total of %d.\n\n\n", total1);

    do {
        puts("Do you think the next roll will be ");
        puts("(H)igher, (L)ower, or (S)ame?\n");
        puts("Enter H, L, or S to reflect your guess.");
        scanf(" %c", &ans);
        ans = toupper(ans);
    } while ((ans != 'H') && (ans != 'L') && (ans != 'S'));

    // Roll the dice a second time to get your second total
    dice1 = (rand() % 5) + 1;
    dice2 = (rand() % 5) + 1;
    total2 = dice1 + dice2;

    // Display the second total for the user
    printf("\nThe second roll was %d and %d, ", dice1, dice2);
    printf("for a total of %d.\n\n", total2);

    if (ans == 'L'){
        if (total2 < total1){
            printf("%d is lower than %d\n", total2, total1);
        }
        else
        {
            printf("Wrong! %d is not lower than %d\n\n", total2, total1);
        }
    }else if (ans == 'H'){
        if (total2 > total1){
            printf("%d is higher than %d\n", total2, total1);
        }else{
            printf("Sorry! %d is not higher than %d\n\n", total2,total1);
        }
    }else if (ans == 'S'){
        if (total2 == total1){
            printf("%d is the same as %d\n\n", total2, total1);
        }else{
            printf("Sorry! %d is not the same as %d\n\n",total2, total1);
        }
    }

    return(0);
}

Result


Related Tutorials