I am making a hangman game and have gotten stuck at the point where the code will detect the given value via scanf and check if it's equal to the hangman word.
if (answer == word[x])
But I keep getting this error
operand types are incompatible ("char" and "char*")
Any solutions to bypass it?
[Edited]
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <time.h>
#define ARRAY_SIZE 10
int main()
{
//randomwordgenerator
char word[ARRAY_SIZE][200] = { "tiger", "lion", "hamster", "zebra", "horse", "camel", "lamb", "borne", "eating", "cat" };
int x = 0;
char mask[200];
srand(time(0));
x = rand() % ARRAY_SIZE;
system("pause");//will pause the rand function
//printf("%s\n", word[x]);
//masking and unmasking word
char m = strlen(word[x]);//will count the number of letters of the random word
strcpy_s(mask, m, word[x]);
for (int i = 0; i < m; i) //loop until all letters are masked
{
mask[i] = '_';
printf("%c ", mask[i]);
}
printf("%d\n", x);//only to show what word in the array 0-9
//Introduction
printf("\nHey! Can you please save me? \n");
printf(" O\n/|\\ \n/ \\ \n");
//Ask for answer
printf("\nType a letter to guess the word and save me. Here are some tips!\n 1) The '_' above are how many letters make up the word, isn't that neat!\n 2) The letters are case sensitive so please pick lower case or I might die\n 3) Have fun! \nNow with that out of the way please type in your guess; ");
char answer[m];
char c;
//loop w counter
int counter; //for the defeat
int found = 0; //for the victory
for (counter = 1; counter < 11; counter ) {
scanf_s("%s", &c);
scanf_s("%s", &answer);
strcpy(answer, &c);
if (strcmp(answer, word[x]) == 0)
{
found ;
}
if (found == x)
{
printf("\n-------------------------\n");
printf(" WIN ");
printf("\n-------------------------\n");
break;
}
if (counter == 2)
{
printf("\n\n\n\n\n\n\n=========\n");//stand1
}
else if (counter == 4)
{
printf("\n \n|\n|\n|\n|\n|\n=========\n");//stand2
}
else if (counter == 6)
{
printf("\n --- \n| |\n|\n|\n|\n|\n=========\n");//stand3 complete
}
else if (counter == 8)
{
printf("\n --- \n| |\n| O\n|\n|\n|\n=========\n");//head
}
else if (counter == 10)
{
printf("\n --- \n| |\n| O\n| |\n|\n|\n=========\n");//body
}
else if (counter == 12)
{
printf("\n --- \n| |\n| O\n| |\n| \\ \n|\n=========\n");//right leg
}
else if (counter == 14)
{
printf("\n --- \n| |\n| O\n| |\n| / \\ \n|\n=========\n");//left leg
}
else if (counter == 16)
{
printf("\n --- \n| |\n| O\n| /| \n| / \\ \n|\n=========\n");//left hand
}
else if (counter == 18)
{
printf("\n --- \n| |\n| O\n| /|\\ \n| / \\ \n|\n=========\n");//right hand
}
else if (counter == 20)
{
printf("\n-------------------------\n");
printf(" LOSE ");
printf("\n-------------------------\n");
printf("\nReally left me hanging there buddy");//Defeat
break;
}
}
return 0;
}
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
CodePudding user response:
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;
}