so I'm having issues with comparing a character with a string inside an if statement. I'm trying to make a not-too-complicated version of Hangman and I'm stuck on this part. I've tried everything from strcmp() and strchr() and others, but nothing seems to work.
#include<stdio.h>
#include<string.h>
int main() {
int length, i;
int size=3;
char word[size]="car";
char letter;
//const char* word="car";
//const char* letter;
int guess;
length=strlen(word);
printf("The given word has %d letters.\n", length);
//do {
guess=0;
printf("Enter a letter:");
scanf("%c", &letter);
for (i=0; i<length; i++) {
if (letter== 'word[i]' && guess==0) {
guess=1;
}
/*if (strchr(letter, word[i])!=NULL && guess==0) {
guess=1;
}*/
}
if (guess=1) {
printf("Correct letter.\n");
}
else {
printf("Wrong letter.\n");
}
//} while ();
}
I'm out of ideas as to what to do and I've searched everywhere as to what I could do. I do not want to use an already-made Hangman game as I'm trying to teach myself as a beginner and because those codes are way too complicated for my simple mind. I'm trying to keep my code simple, and as can be seen in my code above, everything is set up quite simply. I am still a long way from finishing the code, but I just want to find a solution to this problem before I continue.
Thanks!
Aucun commentaire:
Enregistrer un commentaire