# Challenge 2(Dice Roll) Completed :))

 osh Ranw · February 9, 2015 C Programming Tutorial - 38 Challenge ``#include #include #include #include #include int main(){int x = 0;int dice1 ,dice2 ,dice3 ;int sum0,sum1;char val; printf("press '0' or '\$'  to exit!!! \n");    dice1 = (rand()%6 + 1)  ;    dice2 = (rand()%6 + 1) ;    dice3 = (rand()%6 + 1) ;    sum0 = dice1 + dice2 +dice3 ;    printf("------------------------------------\n ");    printf("The Total Value Of The 3 Dice is:%d \n " , sum0 );    printf("------------------------------------\n ");    do{            x++;            if(x != 1){                sum0 = sum1  ;            }            printf(" \n\n Press Higher:h    Lower:l   Same:s \n ");            printf("If you think The Next Time Total Of 3 Dice Would Be:");            scanf(" %s", &val);        if(val == '0'){            printf("Thanks For Playing!!");            break;        }else{        dice1 = rand()%6 + 1  ;        dice2 = rand()%6 + 1 ;        dice3 = rand()%6 + 1 ;        sum1 = dice1 + dice2 +dice3 ;        printf("----------------------------------------------------\n The dice roll Total is:%d  \n----------------------------------------------------\n " , sum1 );        if(val == 's'  || val == 'l' || val == 'h' ){            if(val=='h' && sum0 < sum1 ){                                            printf("Good Job!! \n");                                            continue;            }else if(val == 'l' && sum0 > sum1){                                            printf("Good Job!! \n");                                            continue;            }else if(val == 's'  && sum0==sum1){                                            printf("Good Job!! \n");                                            continue;            }else{                                            printf("You Suck!!!");                                            continue;            };    }else{                printf("You have to press 'h' or 'l' or 's' \n\n");                continue;    }        }    }while(            val != '\$'           );}``

## Replies

- page 2

 osh Ranw · February 9, 2015 Please Could You Explain More? @Lazar Bulic osh Ranw · February 9, 2015 Do i need to change %s to %c? Lazar Bulic · February 9, 2015 Yes that would be correct osh Ranw · February 9, 2015 Thank You osh Ranw · February 12, 2015 @ Lazar Bulic Lazar Bulic · February 9, 2015 But when u put %c and when your program  prompts to input  l, s or h try inputting something like ssssssssssssssssssssssssss Ivo Ferro · February 9, 2015 Hey @osh, every time you insert a value the last character will be an [enter].When you use %s on scanf it expects a special character '\0' that marks the end of the string (vector of chars) .  The '\0' is the final [enter] that you press after inserting the text you want.The problem that you might face when using %c, is that the next variable will consume the [enter]. So if you try to read two chars in a row using scanf("%c", &variable), on the 2nd one you will not be able to insert the value, because you already did when you pressed [enter] on the first one.The solution to use %c, which is more correct than %s when working with characters, will be to consume the [enter].So every time you read a character with %c you can use a funcion like fflush(stdin) that cleans the keyboard's buff, consuming the [enter] and resolving the problem. The problem of fflush(stdin), is that some linux versions don't support it, so you can just use getchar() which is more simple and less efficient.It would be something like that to read two characters:``#include int main(){ char c1, c2; printf("First char?\n"); scanf("%c", &c1); fflush(stdin); //or getchar(); printf("Second char?\n"); scanf("%c", &c2); fflush(stdin); //or getchar(); printf("Your first variable value is %c and your second is %c.\n", c1, c2); return 0;}`` Lazar Bulic · February 13, 2015 Bravo osh Ranw · February 10, 2015 @Ivo Ferro  @Lazar Bulic Thank You i have Modified It now,so it has %c and getchar(); to limit the reading to one,``scanf(" %c", &val);            getchar();``Complete One ``#include #include #include #include #include int main(){int x = 0;int dice1 ,dice2 ,dice3 ;int sum0,sum1;char val; printf("press '0' or '\$'  to exit!!! \n");    dice1 = (rand()%6 + 1)  ;    dice2 = (rand()%6 + 1) ;    dice3 = (rand()%6 + 1) ;    sum0 = dice1 + dice2 +dice3 ;    printf("------------------------------------\n ");    printf("The Total Value Of The 3 Dice is:%d \n " , sum0 );    printf("------------------------------------\n ");    do{            x++;            if(x != 1){                sum0 = sum1  ;            }            printf(" \n\n Press Higher:h    Lower:l   Same:s \n ");            printf("If you think The Next Time Total Of 3 Dice Would Be:");            scanf(" %c", &val);            getchar();        if(val == '0'){            printf("Thanks For Playing!!");            break;        }else{        dice1 = rand()%6 + 1  ;        dice2 = rand()%6 + 1 ;        dice3 = rand()%6 + 1 ;        sum1 = dice1 + dice2 +dice3 ;        printf("----------------------------------------------------\n The dice roll Total is:%d  \n----------------------------------------------------\n " , sum1 );        if(val == 's'  || val == 'l' || val == 'h' ){            if(val=='h' && sum0 < sum1 ){                                            printf("Good Job!! \n");                                            continue;            }else if(val == 'l' && sum0 > sum1){                                            printf("Good Job!! \n");                                            continue;            }else if(val == 's'  && sum0==sum1){                                            printf("Good Job!! \n");                                            continue;            }else{                                            printf("You Suck!!!");                                            continue;            };    }else{                printf("You have to press 'h' or 'l' or 's' \n\n");                continue;    }        }    }while(            val != '\$'           );}``
• 1
• 2

## C

130,868 followers