-1

I'm trying to build a word guesser where you guess a word from the given choice. The words are stored in an array. The player is losing even though they must win.


#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


void guess(int N) {
  int number, guess, numberofguess = 0;
  int myval[] = {"R22", "B3", "R33", "B232", "R12",
                 "B45", "R2", "B12", "R45",  "B32"};
  srand(time(NULL));

  number = rand() % N;
  printf("choose a number "
         "{'R22','B3','R33','B232','R12','B45','R2','B12','R45','B32'}\n");

  char str[20];
  gets(str);
  printf("your guess is %s\n", str);

  if (str == myval[number]) {
    printf("you win\n");
  } else
    printf("you lose\n");

  printf("the number is %s", myval[number]);
}

main() {
  int N = 9;

  guess(N);

  return 0;
}

I want to make the code in such a way when the player enters the code and it matches with the random output the if statement works.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Does the answer at https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c help? – EdmCoff Nov 04 '22 at 15:33
  • Is this code compiles sucessfully really ? Is there any warning or error ? – embeddedstack Nov 04 '22 at 15:35
  • 1
    You're placing strings (`char*`) in an `int[]`, and you should not compare strings with `==` (which will compare the addresses normally) but instead use `strcmp`. – Rogue Nov 04 '22 at 15:36
  • 1
    [The `gets()` function is too dangerous to be used — ever!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) – Jonathan Leffler Nov 04 '22 at 15:47
  • Note that we are no longer in the 20th Century and you should follow the standards that have been in effect for the whole of this millennium. Specifically, you should specify the return type of `main()`. Use `int main(void)` for preference. Although you can use `int main()` it isn't as good as `int main(void)`. See also [What should `main()` return in C and C++?](https://stackoverflow.com/q/204476/15168) – Jonathan Leffler Nov 04 '22 at 15:53
  • 1
    What compiler/version are you using? If your initialization of `myval` and the use of `gets` didn't give you any errors or warnings, the compiler is likely older than you are. It is time to upgrade. – HAL9000 Nov 04 '22 at 16:42

1 Answers1

1
  • Use strcmp while comparing the strings in C.
  • When creating an array from strings, you should create it as char*.
  • Using gets is not good practice
  • You should specify the return type of all functions, including main(), which should return an int.

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>


void guess(int N) {
  int number, guess, numberofguess = 0;
  const char* myval[] = {"R22", "B3", "R33", "B232", "R12",
                 "B45", "R2", "B12", "R45",  "B32"};
  srand(time(NULL));

  number = rand() % N;
  printf("choose a number "
         "{'R22','B3','R33','B232','R12','B45','R2','B12','R45','B32'}\n");

  char str[20];
  gets(str);
  printf("your guess is %s\n", str);

  if (strcmp(str,myval[number]) == 0) {
    printf("you win\n");
  } else
    printf("you lose\n");

  printf("the number is %s", myval[number]);
}

int main() {
  int N = 9;

  guess(N);

  return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
embeddedstack
  • 362
  • 1
  • 13