Challenge Num2 -The dice-

+1 Omar Dalal · August 14, 2014
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
#include <time.h>

int main()
{
    int i;
    int firstDice = 0;
    int secondDice = 0;
    int thirdDice = 0;
    char d = 'a';
    char d1 = 'a';
    char d2 = 'a';
    int sum = 0;
    int sum1 = 0;
    char YN = 'a';
    do{
    printf("Write 'd' to start the first dice roll:");
    scanf(" %c", &d);
    if(d == 'd'){
        srand ( time(NULL) );
        firstDice = (rand() %6) + 1;
        printf("first dice = %d\n", firstDice);
        d = 'a';
    }
    printf("Write 'd' to start the second dice roll:");
    scanf(" %c", &d1);
    if(d1 = 'd'){
        srand ( time(NULL) );
        secondDice = (rand() %6) + 1;
        printf("second dice = %d\n", secondDice);
        d1 = 'a';
    }
    printf("Write 'd' to start the third dice roll:");
    scanf(" %c", &d2);
    if(d2 = 'd'){
        srand ( time(NULL) );
        thirdDice = (rand() %6) + 1;
        printf("third dice = %d\n", thirdDice);
        d2 = 'd';
        break;
    }
    }while(i >= 2);
    sum = firstDice + secondDice + thirdDice;
    printf("total : %d\n", sum);
    printf("Will the second group's total be bigger than the first one?(Y/N): ");
    scanf(" %c", &YN);
    do{
    printf("Write 'd' to start the first dice roll:");
    scanf(" %c", &d);
    if(d == 'd'){
        srand ( time(NULL) );
        firstDice = (rand() %6) + 1;
        printf("first dice = %d\n", firstDice);
        d = 'a';
    }
    printf("Write 'd' to start the second dice roll:");
    scanf(" %c", &d1);
    if(d1 = 'd'){
        srand ( time(NULL) );
        secondDice = (rand() %6) + 1;
        printf("second dice = %d\n", secondDice);
        d1 = 'a';
    }
    printf("Write 'd' to start the third dice roll:");
    scanf(" %c", &d2);
    if(d2 = 'd'){
        srand ( time(NULL) );
        thirdDice = (rand() %6) + 1;
        printf("third dice = %d\n", thirdDice);
        d2 = 'd';
        break;
    }
    }while(i >= 2);
    sum1 = firstDice + secondDice + thirdDice;
    printf("total : %d\n", sum1);
    if(sum1 < sum && YN == 'Y'){
        printf("Try again!Your expectation sucks!");
    }
    else if(sum1 > sum && YN == 'Y'){
        printf("Good expectation!Well done my man!");
    }
    if(sum1 < sum && YN == 'N'){
        printf("Good expectation!Well done my man!");
    }
    else if(sum1 > sum && YN == 'N'){
        printf("Try again!Your expectation sucks!");
    }
    return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Omar Dalal · August 15, 2014
I have experiences with vb, objective-c, python, and I know some of java and C#, and I'm now learning C so I'm not a real professional in C, but I've posted the code because bucky said in his tutorial : when you get done of the challenge that he gave us in the new C programming tutorials post it in the forum!
Thanks again for your reply but I'm still learning so I may get some errors.
Also the main problem I've got is that when I write anything else (not 'd'), the program skips everything and prints out everything on the screen, so this is the problem that I get when I code something.
Btw I'm still new in C programming so I'm gonna learn more and fix my problems!
0 Omar Dalal · August 14, 2014
Thanks for the reply! 
But I'm not getting any errors!  :D
It worked perfectly!
  • 1

C

107,274 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator