My Working Challenge #2

+6 Stacy Simmons · August 26, 2014

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

int main()
{
    int die1, die2, die3;
    int total1, total2;
    char guess, actual;

    srand(time(NULL));

    die1 = (rand() % 6) + 1;
    die2 = (rand() % 6) + 1;
    die3 = (rand() % 6) + 1;

    total1 = die1 + die2 + die3;
    printf("Your rolls were %d %d %d, making your total %d\n", die1, die2, die3, total1);

    printf("Next roll: (h)igher, (l)ower or (s)ame?");
    scanf(" %c", &guess);

    die1 = (rand() % 6) + 1;
    die2 = (rand() % 6) + 1;
    die3 = (rand() % 6) + 1;

    total2 = die1 + die2 + die3;
    printf("Your next rolls were %d %d %d, making your total %d\n", die1, die2, die3, total2);

    if (total2 == total1)
    {
        actual = 's';
    }
    else if (total2 > total1)
    {
        actual = 'h';
    }
    else
    {
        actual = 'l';
    }

    (guess == actual) ? puts("You win!") : puts("You lose!");


    return 0;
}



Edit to improve code appearance (meaning - I just discovered the "code button").

Post a Reply

Replies

Oldest  Newest  Rating
0 Darsh Patel · December 27, 2014
thanks
0 Minh Quan Vu · December 22, 2014
You will understand after watch this video @Darsh Patel:
https://www.youtube.com/watch?v=0Nuhjvxzwro
0 Darsh Patel · December 21, 2014
I did not understand what did formula given below do:
srand(time(NULL));
0 adam wiedeman · December 10, 2014
Ha well done, that is perfect!
0 Minh Quan Vu · November 30, 2014
Thanks
0 Minh Quan Vu · November 30, 2014
 (guess == actual) ? puts("You win!") : puts("You lose!");

I dont understand this line. Can you explain for me please?
  • 1

C

107,111 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator