C语言Hangman游戏:char与char*操作数不兼容报错的解决求助
问题分析与修复方案
原错误根源
你最初用if (answer == word[x])报错,核心原因是:
- 若
answer是单个字符(char类型),word[x]是字符串指针(char*类型),两者类型不匹配,无法直接用==比较; - 就算
answer是字符串,C语言中==比较的是内存地址而非字符串内容,必须用strcmp()函数做内容比对。
但你修改后的代码还有一堆逻辑和语法问题,下面逐个修复:
核心问题修复
变量类型与字符串操作修正
strlen()返回size_t类型,把char m = strlen(word[x]);改成int m = strlen(word[x]);,避免字符类型溢出;strcpy_s(mask, m, word[x]);参数错误,strcpy_s第二个参数是目标缓冲区的总大小,应写成strcpy_s(mask, sizeof(mask), word[x]);(或m+1,因为字符串需要存储终止符\0);- 删除多余的
char c;和重复输入逻辑,Hangman游戏通常是猜单个字母,这里按猜字母逻辑重构输入。
输入读取修正
猜单个字母时,用scanf_s(" %c", &guess, 1);,注意格式串前的空格,用来跳过输入缓冲区的换行符,避免读入空字符。胜利与失败逻辑修正
- 胜利条件:检查
mask是否和目标单词word[x]完全一致,用strcmp(mask, word[x]) == 0判断; - 失败条件:用
wrong_attempts变量统计错误次数,累计到7次时触发失败(对应吊死图的完整绘制步骤),原代码的循环counter <11根本到不了20,逻辑完全失效。
- 胜利条件:检查
吊死图绘制逻辑修正
根据错误次数wrong_attempts绘制对应阶段的图形,而非循环次数,每次错误后更新图形。
完整修正代码
#include<stdio.h> #include<string.h> #include<stdlib.h> #include <time.h> #define ARRAY_SIZE 10 int main() { // 随机单词数组 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"); // 暂停让随机数生效 // 初始化掩码:先复制单词,再替换为下划线 int m = strlen(word[x]); strcpy_s(mask, sizeof(mask), word[x]); for (int i = 0; i < m; ++i) { mask[i] = '_'; printf("%c ", mask[i]); } printf("\n"); // 换行更整洁 // 游戏介绍 printf("\nHey! Can you please save me? \n"); printf(" O\n /|\\\n / \\\n"); // 提示信息 printf("\nType a letter to guess the word and save me. Here are some tips!\n"); printf(" 1) The '_' above are how many letters make up the word, isn't that neat!\n"); printf(" 2) The letters are case sensitive so please pick lower case or I might die\n"); printf(" 3) Have fun! \nNow with that out of the way please type in your guess: "); char guess; int wrong_attempts = 0; int is_win = 0; // 游戏主循环:最多7次错误机会 while (wrong_attempts < 7) { // 读取单个字母,处理换行符 scanf_s(" %c", &guess, 1); int found = 0; // 检查字母是否在单词中,更新掩码 for (int i = 0; i < m; ++i) { if (word[x][i] == guess) { mask[i] = guess; found = 1; } } // 打印当前掩码状态 printf("\nCurrent progress: "); for (int i = 0; i < m; ++i) { printf("%c ", mask[i]); } printf("\n"); // 判断是否猜错,更新错误次数并绘制图形 if (!found) { wrong_attempts++; printf("\nWrong guess! You have %d attempts left.\n", 7 - wrong_attempts); switch (wrong_attempts) { case 1: printf("\n\n\n\n\n\n=========\n"); break; case 2: printf("\n+\n|\n|\n|\n|\n|\n=========\n"); break; case 3: printf("\n+---+\n| |\n|\n|\n|\n|\n=========\n"); break; case 4: printf("\n+---+\n| |\n| O\n|\n|\n|\n=========\n"); break; case 5: printf("\n+---+\n| |\n| O\n| |\n|\n|\n=========\n"); break; case 6: printf("\n+---+\n| |\n| O\n| |\n| / \\\n|\n=========\n"); break; case 7: printf("\n+---+\n| |\n| O\n| /|\\\n| / \\\n|\n=========\n"); break; } } // 检查是否胜利 if (strcmp(mask, word[x]) == 0) { is_win = 1; break; } } // 输出最终结果 if (is_win) { printf("\n-------------------------\n"); printf(" WIN "); printf("\n-------------------------\n"); } else { printf("\n-------------------------\n"); printf(" LOSE "); printf("\n-------------------------\n"); printf("\nReally left me hanging there buddy!\n"); printf("The correct word was: %s\n", word[x]); } return 0; }
关键改动说明
- 用
wrong_attempts统计错误次数,对应吊死图的7个绘制阶段,逻辑清晰; - 猜单个字母时遍历目标单词更新掩码,符合Hangman游戏的常规玩法;
- 修复
scanf_s的使用方式,处理输入缓冲区的换行符,避免无效输入; - 胜利条件改为比对掩码和目标单词的内容,逻辑正确;
- 修正字符串操作的参数错误,避免内存越界风险。
内容的提问来源于stack exchange,提问作者Ocelotter
相关产品推荐
相关产品推荐

