# 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 1

 Lazar Bulic · February 10, 2015 Unfortunate you misunderstood @Ivo. The problem is  when user inputs something like ffffffffffffffffffffffffffffffffff. Even in your corrected code it isn't fixed.Here is the solution:``char val, ch;scanf("%c", &val);while (( ch = getchar()) != '\n' && ch != EOF);`` c student · February 10, 2015 @Ivo fflush() is not part of standard c and fflush (stdin) produces undefined behaviour.  don't use it if you can avoid it.@Lazar getchar() returns an int value, therefore in your code, using an unsigned char value may not sufficient enough to handle an EOF return on some systems. Ivo Ferro · February 10, 2015 Thanks for all the feedbacks Lazar Bulic · February 10, 2015 @c student Thanks  the solution is ``int c;while ((c = getchar()) != EOF && c != '\n')`` J show · February 14, 2015 Write a program that reads alphanumeric characters from the keyboard, and computes the average ascii value of the alpha numeric characters, the average alphabetical character, the average numeric character and the average uppercase character. Outputting each, you program should terminate reading once it read a non-alphanumeric character. How would you guys go about doing this problem? using ctype.h and isalnum? Reynaldo Gunawan · June 2, 2015 Hey guys, i've been trying to solve this challenge but my program seems to keep insisting that the user has guessed the right thing,  even though it hasn't. Can anyone help me with the code i made? http://textuploader.com/7g9kI would appreciate any related answers to this challenge. Thanks Reynaldo Gunawan · June 13, 2015 OMG I feel so stupid. Thank you for your replies c student · June 3, 2015 ``if(user = true){    /* should be comparison operator here */            printf("Good Job!\n");        }else{            printf("Meh, you tried!\n");        }`` osh Ranw · June 3, 2015 if(user = true){            printf("Good Job!\n");        }else{            printf("Meh, you tried!\n");        }    }    ---------------------------------------------------------------------------------------change it to if(user == true){            printf("Good Job!\n");        }else{            printf("Meh, you tried!\n");        }    } Lazar Bulic · February 9, 2015 You are scanning  a string type in to a character type variable. ``scanf(" %s", &val);``
• 1
• 2

## C

129,680 followers
About

One of the most popular languages of all time.

Links
Moderators
 Bucky Roberts Administrator