C diceroll game

97 views Asked by At

I wanted to create a dice game but it's always saying my guess is wrong and idk why. I finally understand how to make the rand function actually random btw :D I think it's because the array isn't storing the strings correctly.

int main()
{

srand(time(NULL));
int dice1 = 1, dice2 = 1, dice3 = 1, sum, key = 0;
int dice4 = 1, dice5 = 1, dice6 = 1, sum2;
char randomnumber[10];

printf("Diceroll-game \n\n");
printf("The first sum is:\n");


dice1 = (rand()%6 + 1);
dice2 = (rand()%6 + 1);
dice3 = (rand()%6 + 1);

sum = dice1 + dice2 + dice3;

printf("%d\n", sum);

printf("Will the next sum of 3 dices be higher(hi), lower (lo) or the same(sa) (hi, lo, sa)?\n");
printf("Type hi, lo or sa\n");

scanf("%c \n", &randomnumber);

dice4 = (rand()%6 + 1);
dice5 = (rand()%6 + 1);
dice6 = (rand()%6 + 1);

sum2 = dice4 + dice5 + dice6;

if (randomnumber == "hi" && sum2 > sum) {
   printf("You were right !\n\a");

}else {
    if (key == 0) {
printf("You were wrong.");
key = 1;
}
}


if (randomnumber == "lo" && sum2 < sum) {
   printf("You were right !\n\a");

}else {
    if (key == 0) {
printf("You were wrong.");
key = 1;
}
}


if (randomnumber == "sa" && sum2 == sum) {
   printf("You were right !\n\a");

}else {
    if (key == 0) {
printf("You were wrong.");
key = 1;
}
}



printf("\nIt's %d", sum2);

return 0;

Can someone help me please. Thank you.

2

There are 2 answers

0
Natasha Dutta On
  • Firstly, You're using wrong format specifier here. %c is used to scan one char. You need to use %s for a string. Using wrong format specifier invokes undefined behaviour.

    Then, the format string you supply to scanf() needs to match the input exactly. You don't need a newline there.

    Also, randomnumber being an array, adding & while using that as scanf() argument is not required.

    Change

    scanf("%c \n", &randomnumber);
    

    to

    scanf("%s", randomnumber);
    
  • Secondly, string comaprisons cannot be done using == operator. You need to use strcmp() to compare two string contents.

    Change

    .. randomnumber == "hi" ...
    

    to

    ..  !strcmp(randomnumber, "hi")   ...
    
2
Spikatrix On

Three problems:

  1. You are using the wrong format specifier for scanning a string. %c is used for a char while %s is used for strings. So change

    scanf("%c \n", &randomnumber);
    

    to

    scanf("%s", randomnumber);
    

    Other changes I made is removing the \n and space character as it will not stop scanning until a non-whitespace character, which is probably not what you want. Also, %s expects a char*. &randomnumber is of type char(*)[10] while randomnumber gets converted to &randomnumber[0], a char*, which is exactly what %s wants to have.

  2. In C, when you compare strings using ==, you compare pointers and not the actual content. To compare the contents, include string.h and use strcmp. It returns 0 if both the strings are equal. So change the following:

    if (randomnumber == "hi" && sum2 > sum) {
    if (randomnumber == "lo" && sum2 < sum) {
    if (randomnumber == "sa" && sum2 == sum) {
    

    to

    if (strcmp(randomnumber, "hi") == 0 && sum2 > sum) {
    if (strcmp(randomnumber, "lo") == 0 && sum2 < sum) {
    if (strcmp(randomnumber, "sa") == 0 && sum2 == sum) {
    
  3. Now, after you've fixed the other problems, you'll notice that some things print twice, which is not probably what you want. The reason is that you have three ifs with strcmps. Only one can be true at most which will print "You were right !\n" and the rest will print "You were wrong.". Fix it by changing

    if (strcmp(randomnumber, "hi") == 0 && sum2 > sum) {
       printf("You were right !\n\a");
    
    }else {
        if (key == 0) {
    printf("You were wrong.");
    key = 1;
    }
    }
    
    
    if (strcmp(randomnumber, "lo") == 0 && sum2 < sum) {
       printf("You were right !\n\a");
    
    }else {
        if (key == 0) {
    printf("You were wrong.");
    key = 1;
    }
    }
    
    
    if (strcmp(randomnumber, "sa") == 0 && sum2 == sum) {
       printf("You were right !\n\a");
    
    }else {
        if (key == 0) {
    printf("You were wrong.");
    key = 1;
    }
    

    to

    if (strcmp(randomnumber, "hi") == 0 && sum2 > sum) {
       printf("You were right !\n\a");
    }else if(strcmp(randomnumber, "lo") == 0 && sum2 < sum) {
       printf("You were right !\n\a");
    }else if(strcmp(randomnumber, "sa") == 0 && sum2 == sum) {
       printf("You were right !\n\a");
    }else {
        if (key == 0) {
           printf("You were wrong.");
        key = 1;
    }
    

Note that the key variable is superfluous in your program.