Generating a dice game - C Programming

1.2k views Asked by At

I'm following a tutorial on youtube and was doing a dice generator. It basically print out 3 dice result and sum out the dice result. After which, the user will look at the sum, and based on the sum, the user going to guess whether the next roll is going to be higher,lower, or the same.

Below is my code, suppose, when I typed 'yes', it should be doing the code inside the if statement. However, it went straight to the else statement. Can someone please tell me what's wrong?

int answer;
int guess;
int diceRoll4 = 0;
printf("Would you like to guess your next dice? Y/N \n");
scanf(" %c", &answer);

if (answer == 'yes' ){

    printf("What is your guess?\n");
    printf("please key in your number \n");
    scanf(" %d", &guess);
    if (guess > diceRoll4 ){
        printf(" You got it wrong, too high!");
    }
    else if (guess < diceRoll4){
            printf(" You got it wrong, too low!");
    }
    else {
        printf("You got it right");
    }

}
else{
    printf("Thanks for playing");
}
5

There are 5 answers

0
Spikatrix On BEST ANSWER

First of all, answer should be an array of chars in order to hold a string. Change

int answer;

to

char answer[10]; //Or any other reasonable size

Secondly, since you want to scan a string and not a character, change

scanf(" %c", &answer);

to

scanf("%9s", answer);

The 9 will scan a maximum of 9 characters (+1 for the NUL-terminator at the end), thus preventing buffer overflows.
I've removed & as %s expects a char* while &answer will give a char(*)[10]. Name of an array gets converted into a pointer to its first element char*, exactly what %s expects. The above scanf is thus equivalent to

scanf("%9s", &answer[0]);

Thirdly, comparing two strings using == compares pointers and not the actual content in them. Use strcmp from string.h instead. It returns 0 when both its arguments hold the same content. Change

if (answer == 'yes' ){

to

if (strcmp(answer, "yes") == 0){

Double quotes are used to denote a NUL-terminated string(char*), which is exactly what strcmp expects, while single quotes, as in your code, is a multi-character literal whose value is implementation-defined.

0
user3629249 On

this line:

if (answer == 'yes' ){

has several problems.

1) the definition of 'answer' is 'int' but the scanf is inputting a single character

2) answer could be compared with 'y' or 'n' but not to a array of char.

3) since the scanf only input a single char 
   and you/the user input 'yes', 
   only the first character was consumed, 
   so the 'es' are still in the input buffer

4) note the the single character could be anything, except white space.
   the leading space in the format string would consume any white space.
   so the user could input say 'y' or 'Y'  
   these are different characters
   however, using the toupper() macro from ctypes.h
   would mean only a 'Y' would need to be compared

5) if you decide to read a string, 
   then 'answer' needs to be a character array, 
   say:  char answer[10];
   and the scanf needs to have a max length modifier 
   on the associated "%s" input/conversion parameter
   so as to avoid the user overflowing the input buffer
   and the comparison would be via the strcmp() function

6) always check the returned value (not the parameter value) 
   from scanf to assure the operation was successful

7) diceRoll4 and guess can never be a negative number
   so the variable definitions should be unsigned
   and the associated scanf() for guess should use 
   something like "%u"

8) on the printf() format strings, always end them with '\n' 
   so the sting will be immediately displayed to the user, 
   otherwise, they will only be displayed 
   when a input statement is executed or the program exits
0
adricadar On

To test the equality you have to use strcmp. If the returning value is 0 it means that they are equal.

if (strcmp(answer, "yes") == 0) {
    // ...
} else {
    // ...
}

Notes:

  1. Using just answer == 'yes' it test the equality of pointers not value. This is the reason why enters only in else.

  2. Because answer is int you have to change to an array

    char answer[15]
    
  3. As @Sathya mentioned you are reading just a char %c for reading a string you have to use %s

    scanf("%s", answer);
    
  4. Instead of 'yes' which is multi-character character constant change to "yes" that is an array of char with \0 at the end, more informations here.

0
P.P On

'yes' is a multi-byte character whose behaviour is implementation-defined.

What you probably want is to read and compare a single char:

if (answer == 'y' ){

or read a whole string and compare:

char answer[128];
scanf("%s", answer);
if ( strcmp(answer,"yes") == 0 ){
...
}

Notice that I changed the type of answer and used %s to read a string.

0
lord.garbage On

If you do not want to read in a string, but only a single char where the user can answer either Y or N, you should change int answer; to char answer;. You can then go on using your original scanf()-call. You will still need to change

if (answer == 'yes')

to

if (answer == 'Y')

If you want the user to either type in y or Y you could user toupper() from ctype.h and change your if-condition to if (toupper(answer) == 'Y').