I'm still really new to the C language and I'm trying to make a hangman game but I keep failing to end the game when I win.
Here is the code:
const int true = 1; const int false = 0; char words[][20] = { "hangman", "computer", "programming", "microsoft", "visual", "studio", "express", "learning" }; int isletterinword(char word[], char letter) { int i; for (i = 0; i < strlen(word); i++) { if (word[i] == letter) { return true; } } return false; } int iswordcomplete(char secretword[], char rights[]) { int i; for (i = 0; i < strlen(secretword); i++) { if (rights[i] == secretword[i] ) { return true; } } return false; } void printhangman(int numofwrongs) { // Line 1 printf("\t ______\n"); // Line 2 printf("\t | |\n"); // Line 3 printf("\t | +\n"); // Line 4 - left arm, head and right arm printf("\t |"); if (numofwrongs > 0) printf(" \\"); if (numofwrongs > 1) printf("O"); if (numofwrongs > 2) printf("/"); printf("\n"); // Line 5 - body printf("\t |"); if (numofwrongs > 3) printf(" |"); printf("\n"); // Line 6 - left leg and right leg printf("\t |"); if (numofwrongs > 4) printf(" /"); if (numofwrongs > 5) printf(" \\"); printf("\n"); // Line 7 printf("\t |\n"); // Line 8 printf("\t__|__\n"); } void printletters(char letters[]) { int i; for (i = 0; i < strlen(letters); i++) { printf("%c ", letters[i]); } } void printscreen(char rights[], char wrongs[], char secretword[]) { int i; for (i = 0; i < 25; i++) printf("\n"); printhangman(strlen(wrongs)); printf("\n"); printf("Correct guesses: "); printletters(rights); printf("\n"); printf("Wrong guesses: "); printletters(wrongs); printf("\n\n\n"); printf("\t"); for (i = 0; i < strlen(secretword); i++) { if (isletterinword(rights, secretword[i])) { printf("%c ", secretword[i]); } else { printf("_ "); } } printf("\n\n"); } int main() { int i; int secretwordindex; char rights[20]; char wrongs[7]; char guess; secretwordindex = 0; srand(time(0)); secretwordindex = rand() % 8; for (i = 0; i < 20; i++) { rights[i] = '\0'; } for (i = 0; i < 6; i++) { wrongs[i] = '\0'; } while (strlen(wrongs) < 6) { printscreen(rights, wrongs, words[secretwordindex]); printf("\nPlease enter your guess: "); scanf(" %c", &guess); if (isletterinword(words[secretwordindex],guess)) { rights[strlen(rights)] = guess; } else { wrongs[strlen(wrongs)] = guess; } } printscreen(rights, wrongs, words[secretwordindex]); if ( iswordcomplete(words[secretwordindex],rights[20])==true && strlen(wrongs) <= 6 ) { // The if condition here might be problematic. printf("You have won!\n"); } else { printf("You have lost!\n"); } }
Here is the error message:
https://stackoverflow.com/questions/65756665/get-a-problem-when-make-a-hangman-game-c-language January 17, 2021 at 10:10AMmain.c:197:48: warning: passing argument 2 of 'iswordcomplete' makes pointer from integer without a cast [-Wint-conver sion]
main.c:55:5: note: expected 'char *' but argument is of type 'char'
没有评论:
发表评论