# #38 challenge 2 : Dice game (help please!)

 Ellie Shin · March 15, 2015 My code would always print "Try harder" as ifif(guess == 'h' && sum#include #include #include #include int main(){    // get the sum of 3 dice prnt out the total    // use guess if its higher (h) or lower or the same    // roll the dice again    // if right, good job wrong, you suck    int i = 0;    int sum = 0;    char guess[2];    for(i = 0; i; i++){        sum +=(rand()%6)+1;        if(i==2){            printf("The sum is %d \n",sum);        }    }    puts("Enter your guess for the next sum: higher, lower, or the same. (h/l/s)");    gets(guess);    int sum2 = 0;    for(i = 0; i; i++){        sum2 +=(rand()%6)+1;        if(i==2){            printf("The sum is %d \n",sum2);        }    }    printf("%s\t%d\t%d \n",guess,sum,sum2);    if(guess == 'h' && sumsum2){        printf("Good job!");    }else if(guess == 's' && sum==sum2){        printf("Good job!");    }else{        printf("Try harder!");    }    return 0;}

## Replies

 Dol Lod · March 15, 2015 You have inconsistent code regarding comparisons. First, you compare the value at guess[0] with the character h, then compare guess with the string l, not the character l. Please be careful when writing your code. I decided to make the code less ambiguous.  You are also using unnecessary libraries like string. I think you should reduce down to the libraries you actually use. To fix the for loop comparison now showing up, I replaced  with <=2. If you want to, change it again while testing. ``#include #include #include #include #include int main(){ // get the sum of 3 dice prnt out the total // use guess if its higher (h) or lower or the same // roll the dice again // if right, good job wrong, you suck int i = 0; int sum = 0; char guess[2]; for(i = 0; i<=2; i++) { sum +=(rand()%6)+1; } printf("The sum is %d \n",sum); puts("Enter your guess for the next sum: higher, lower, or the same. (h/l/s)"); gets(guess); guess[1]='\0'; int sum2 = 0; for(i = 0; i<=2; i++) { sum2 +=(rand()%6)+1; } printf("The sum is %d \n",sum2); printf("%s\t%d\t%d \n",guess,sum,sum2); if(guess[0] == 'h' && sumsum2) { printf("Good job!"); } else if(guess[0] == 's' && sum==sum2) { printf("Good job!"); } else { printf("Try harder!"); } return 0;}`` Ellie Shin · March 15, 2015 i used both the string and the char to try differnt things and forgot to fix it back thank you so much! OLayemii Garuba · March 16, 2015 #include #include #include #include #include #include int main(){// get the sum of 3 dice prnt out the total// use guess if its higher (h) or lower or the same// roll the dice again// if right, good job wrong, you sucksrand (time (NULL));int i = 0;int sum = 0;char guess;for(i = 0; i ; i++){sum +=(rand()%6)+1;if(i==2){printf("The sum is %d \n",sum);}}puts("Enter your guess for the next sum:higher, lower, or the same.(h/l/s)"); scanf("%c",&guess);int sum2 = 0;for(i = 0; i ;i++){sum2 +=(rand()%6)+1;if(i==2){printf("The sum is %d \n",sum2);}}printf("%c\t%d\t%d\n",guess,sum,sum2);if (guess == 'h' && sum2 > sum){printf("Good job!"); }else if (guess == 'l' && sum>sum2){printf("Good job!");}else if (guess== 's' && sum==sum2){printf("Good job!");}else{printf("Try harder!");}return 0;}this works!! OLayemii Garuba · March 16, 2015 and it keeps bringing up same sum cause you failed to seed your random numbers that's where //stand (time(NULL)) comes in place! Jacky Leung · March 29, 2015 #include #include int ThrowDice(){    int dice, sum, i;    puts("Throwing dice...");    sum = 0;    for (i = 0; i < 3; i++)        sum += rand() % 6 + 1;    return sum;}int main(){    int checkpoint, thistime;    char ans, corr;    thistime = ThrowDice();    puts("\n****************************\n");    do{        checkpoint = thistime;        printf("Last time: %d \n", checkpoint);        puts("How about this time: Higher(h)? Lower(l)? Same(s)? Give up(g)?");        scanf(" %c", &ans);        if (ans != 'h' && ans != 'l' && ans != 's')            break;        thistime = ThrowDice();        printf("This time: %d \n", thistime);        if (thistime>checkpoint){            corr = 'h';            printf("Higher - ");        }else if (thistime
• 1

## C

124,512 followers