windows - C diceroll game -


i wanted create dice game it's saying guess wrong , idk why. understand how make rand function random btw :d think it's because array isn't storing 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 next sum of 3 dices higher(hi), lower (lo) or 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 right !\n\a");  }else {     if (key == 0) { printf("you wrong."); key = 1; } }   if (randomnumber == "lo" && sum2 < sum) {    printf("you right !\n\a");  }else {     if (key == 0) { printf("you wrong."); key = 1; } }   if (randomnumber == "sa" && sum2 == sum) {    printf("you right !\n\a");  }else {     if (key == 0) { printf("you wrong."); key = 1; } }    printf("\nit's %d", sum2);  return 0; 

can me please. thank you.

  • firstly, you're using wrong format specifier here. %c used scan 1 char. need use %s string. using wrong format specifier invokes undefined behaviour.

    then, format string supply scanf() needs match input exactly. don't need newline there.

    also, randomnumber being array, adding & while using scanf() argument not required.

    change

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

    to

    scanf("%s", randomnumber); 
  • secondly, string comaprisons cannot done using == operator. need use strcmp() compare 2 string contents.

    change

    .. randomnumber == "hi" ... 

    to

    ..  !strcmp(randomnumber, "hi")   ... 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -