Challenge 2(Dice Roll) Completed :))

+1 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 != '$'
           );





}



http://i1158.photobucket.com/albums/p604/Oshadha4/Capture_zpstbpdm4kj.png



http://www.troll.me/images2/fist-baby/yes-finally.jpg




Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 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);
0 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.
0 Ivo Ferro · February 10, 2015
Thanks for all the feedbacks :)
0 Lazar Bulic · February 10, 2015
@c student Thanks :) the solution is :D

int c;
while ((c = getchar()) != EOF && c != '\n')
0 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?
0 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/7g9k

I would appreciate any related answers to this challenge. 

Thanks
0 Reynaldo Gunawan · June 13, 2015
OMG I feel so stupid. Thank you for your replies
0 c student · June 3, 2015
if(user = true){    /* should be comparison operator here */
           printf("Good Job!\n");
       }else{
           printf("Meh, you tried!\n");
       }
0 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");
       }
   }
  
0 Lazar Bulic · February 9, 2015
You are scanning  a string type in to a character type variable. 


scanf(" %s", &val);
  • 1
  • 2

C

107,156 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator