First off, you were using the strcpy_s
function wrong, you didn’t give the length of the string. So I changed it like;
strcpy_s(mask, m, word[x]);
And also, the format specifier we’re going to use while getting a string from the user will be %s
like;
scanf_s("%s", &answer);
Finally, I prefer using strcmp
function to compare two strings, if these strings are equal then the result would be 0
, so the if
statement changes like;
if (strcmp(&answer, word[x]) == 0)
EDIT: Ok so if you can’t use the if
statement with the input from the user, we can get the input, copy that into another string and then compare these two strings. So I fixed it like;
char answer[m];
char c;
//loop w counter
int counter;
for (counter = 1; counter < 11; counter++) {
scanf_s("%s", &c);
strcpy(answer, &c);
if (strcmp(answer, word[x]) == 0)
{
printf("\nYou win! The answer was: %s", word[x]);
return 0;
}
We get an input with c
, copy the string into an answer
array, BTW I’m not sure about its size so you can modify it, and then compare these two strings in the strcmp
function.
EDIT: We don’t need another string and copy that into an array, instead, we can directly get the array from the user and just compare it with the word[x]
. So here’s the latest version:
char answer[ARRAY_SIZE];
//loop w counter
int counter;
for (counter = 1; counter < 11; counter++) {
scanf_s("%s", &answer);
if (strcmp(answer, word[x]) == 0)
{
printf("\nYou win! The answer was: %s", word[x]);
return 0;
}
17
solved C language, Hangman game, if question, “char” and “char*” operand issue when comparing answer(the user input) == word(hangman word) [closed]