C Challenge #2 Help !!

0 Sharad Gupta · September 6, 2014
Hey guyz..I am posting this code as a part of challenge #2 in C language.

In this challenge, user throws 3 dice (using rand() function), the sum of which is displayed on the screen, user then guesses the outcome of the second dice roll act, by pressing High = H / Low = L / Same = S.

As per user's guess, appropriate message is displayed.

In this code, I want the program to give a response to the user if he/she enters the wrong character instead of 'H' / 'L'/ 'K',


When I did it, I entered multiple characters to check instead of just H/L/S and also combinations involving these characters like 'JLKJLJJJ'
but the message 'Guess the probable..' was displayed as many times the number of characters I entered. 


If the complier picked up 'L' as from the above input, the program was executed which should NOT be done because the overall the input by user was wrong !!

Please tell me how can I fix this !!



#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
int main()
{
    int diceA, diceB, diceC;
    int sum1 = 0, sum2 = 0;
    char val, choice='Y';

    do
    {
     while(choice!='N')
     {
     printf("Do you want to play this Dice Game? (Y/N) \n");
     scanf(" %c", &choice);
     if(choice=='Y')
        break;
     }

     if(choice=='Y')
     {

    /*Seeds rand() function to produce pseudo-random numbers,
     Bucky you didn't mention that..hehe !!*/

     srand(time(NULL));

    //For the first roll

     diceA = (rand()%6) + 1;
     diceB = (rand()%6) + 1;
     diceC = (rand()%6) + 1;

     printf("Dice A Roll results in No. %d \n", diceA);
     printf("Dice B Roll results in No. %d \n", diceB);
     printf("Dice C Roll results in No. %d \n", diceC);

     sum1 = diceA + diceB + diceC;

     printf("\nSum of three rolls is = %d \n", sum1);

     printf("Guess the probable result for next dice roll (H=High)/(L=Low)/(Same=S) ! \n");
     scanf(" %c", &val);

     //For the second roll

     diceA = (rand()%6) + 1;
     diceB = (rand()%6) + 1;
     diceC = (rand()%6) + 1;

     printf("\nDice A Roll results in No. %d \n", diceA);
     printf("Dice B Roll results in No. %d \n", diceB);
     printf("Dice C Roll results in No. %d \n", diceC);

     sum2 = diceA + diceB + diceC;
     printf("\nSum of three rolls is = %d \n", sum2);

     switch(val)
     {
        case 'H':
        if(sum2 > sum1)
        {
            printf("\nCool, you guessed it right !!\n");
        }
        else
        {
            printf("\nDumb, you suck !!\n");
        }
        break;


        case 'L':
        if(sum2 < sum1)
        {
            printf("\nCool, you guessed it right !!\n");
        }
        else
        {
            printf("\nDumb, you suck !!\n");
        }
        break;


        case 'S':
        if(sum2 == sum1)
        {
            printf("\nCool, you guessed it right !!\n");
        }
        else
        {
            printf("\nDumb, you suck !!\n");
        }
        break;
     }
     }
     else
     {
        printf("\nThank you, See you again!! \n");
     }

    }while(choice!='N');

    return 0;
}


Post a Reply

Replies

Oldest  Newest  Rating
+2 Karan Desai · September 8, 2014
You have taken the variable 'val' of char data type. You can use array of char instead. For eg. Declare char val. Then to validate correct user input, you can put if condition as:
if(val[0]!=H || val[0]!=L || val[0]!=S) 
{
reenter()...
}

This way, by array of char, your condition prevent user to enter any other char or group of characters...

Just take care, when using array of char, during scanf you will have to modify scanf("%c",&val[0])...

I hope this works.

Good Luck!:)
  • 1

C

107,067 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator