Challenge 2 completed

+2 Горан Куга · October 21, 2014
Hi guys. I'm done with challenge 2 waiting for your suggestions on my code. I also provide same code but with comments http://pastebin.com/Eh8whA9X.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <windows.h>
#include <time.h>
 
/*
Challenge #2 Buckys Room
Rolling dice by Goran
*/
 
int main()
{
int diceA, diceB, diceC, diceSumFirstTime, diceSumSecondTime;
char answer[7];
 
printf("Are you ready to roll the dice ? ");
scanf(" %s", answer);
answer[0] = toupper(answer[0]);
 
if(answer[0] == 'Y'){
srand (time(NULL));
diceA = (rand() % 6) + 1;
diceB = (rand() % 6) + 1;
diceC = (rand() % 6) + 1;
 
printf("\nDice A: %d \n", diceA);
Sleep(1000);
printf("Dice B: %d \n", diceB);
Sleep(1000);
printf("Dice C: %d \n", diceC);
Sleep(1000);
 
diceSumFirstTime = diceA + diceB + diceC;
printf("Dice Sum: %d \n \n", diceSumFirstTime);
Sleep(1000);
 
printf("Will the sum of next three dices be higher, lower or same ? ");
scanf(" %s", answer);
answer[0] = toupper(answer[0]);
 
if(answer[0] == 'H' || answer[0] == 'L' || answer[0] == 'S'){
printf("Rolling for the second time. \n");
Sleep(1000);

diceA = (rand() % 6) + 1;
diceB = (rand() % 6) + 1;
diceC = (rand() % 6) + 1;
 
printf("\nDice A: %d \n", diceA);
Sleep(1000);
printf("Dice B: %d \n", diceB);
Sleep(1000);
printf("Dice C: %d \n", diceC);
Sleep(1000);
 
diceSumSecondTime = diceA + diceB + diceC;
printf("Dice Sum: %d \n \n", diceSumSecondTime);
Sleep(1000);
 
if(diceSumFirstTime < diceSumSecondTime && answer[0] == 'H'){
printf("Well done. Sum is higher then first roll. \n");
}else if(diceSumFirstTime > diceSumSecondTime && answer[0] == 'L'){
printf("Well done. Sum is lower then first roll. \n");
}else if(diceSumFirstTime == diceSumSecondTime && answer[0] == 'S'){
printf("Well done. Sum is the same as first roll. \n");
}else{
printf("Just unlucky. \n");
}
 
}else{
printf("Maybe try next time. \n \n");
}
}else{
printf("Maybe try next time. \n \n");
}
 
system("pause");
return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
+1 Daniel Bogorad · October 28, 2014
hy everyone !
Goran - nice job :) !

Below is my version of the dice roll game :-) 



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


int main()
{
    int i , dice[5] , diceSum1 , diceSum2;
    char start, reroll, guess ;
    printf("Dice roll game. Press r to start \n");
    scanf(" %c", &start);

    if (start == 'r'){
        for(i = 0 ; i <=2 ; i++){
           dice = (rand()%6) + 1 ;
        }

        diceSum1 = dice[0]+dice[1]+dice[2];
        printf("The sum of 3 dice throws is : \t %d \n", diceSum1 );
        printf("Now the dice will roll again three times, press r  , \n");
        scanf(" %c", &reroll);

        if (reroll == 'r'){
            for(i = 3 ; i <=5 ; i++){
               dice = (rand()%6) + 1 ;
            }
            diceSum2 = dice[3]+dice[4]+dice[5];

            printf("\n Is the second dice sum higher , lower , the same as the previous ?");
            printf("Enter your guess here :  (h/l/s)");
            scanf(" %c", &guess);

                if((diceSum1 > diceSum2) && (guess == 'l')){
                   printf("Nice - You are correct ! The second dice sum was %d\n", diceSum2);
                }else if((diceSum1 > diceSum2) && ((guess == 'h')||(guess == 's'))){
                    printf("The answer is wrong ! Bad guess ! The second dice sum was %d\n", diceSum2);
                }else{
                    if ((diceSum2 > diceSum1) && (guess == 'h')){
                        printf("Nice - You are correct ! The second dice sum was %d\n", diceSum2);
                    }else if ((diceSum2 > diceSum1) && ((guess == 'l')||(guess == 's'))){
                        printf("The answer is wrong ! Bad guess ! The second dice sum was %d\n", diceSum2);
                    }else if ((diceSum1 == diceSum2) && (guess == 's')){
                        printf("Nice - You are correct ! The second dice sum was %d\n", diceSum2);
                    }else if ((diceSum1 == diceSum2) && ((guess == 'l')||(guess == 'h')))
                        printf("The answer is wrong ! Bad guess ! The second dice sum was %d\n", diceSum2);
                }
        }
    }
    return 0;
}

id be glad to hear your comments ))
thanks :)
0 Горан Куга · October 25, 2014
I meant counting from zero and not one ? Also i don't get a crash in release mode ?
0 Горан Куга · October 24, 2014

The size of string "Higher" is 7 bytes(including null terminator). because it consists of 7 char's ans sizeof(char) is equal to 1. You actually can print std::cout << sizeof("Higher"); and it will give you 7. What is the reason you think elements count from zero?


This is the reason why i think elements count from zero. I fixed it in my code. But still im so confused right now. Can you please explain it why this happens on image ?

http://i.imgur.com/ww2fBSS.png
0 Горан Куга · October 24, 2014
Yes 5 bytes ?

Higher counting ?
H = 0
i = 1
g = 2
h = 3
e = 4
r = 5

0 Горан Куга · October 24, 2014

What is the size of string "Higher"?

Other than that the code looks ok.


5 bytes and one for string terminator that's why it's for char password 6.
0 Горан Куга · October 23, 2014
No suggestions or questions ?
  • 1

C

107,174 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator