终端无输出的C语言猜单词游戏代码排查请求
Hangman猜单词游戏代码无输出问题排查
我正在开发课程作业Hangman(猜单词)游戏,作业今日截止,但代码突然不再在终端输出内容,请求帮忙排查代码以完成提交。
原代码:
char **readWordList(char *, int *); int main(int argc, char *argv[]) { char **wordList; char inputFile[100]; int count = 0; int i; if (argc != 2) { printf("You need to provide the word list file name. "); printf("Usage: $0 filename\n"); return -1; } wordList = readWordList(argv[1], &count); //函数(目标输入,计数占位变量) if (wordList == NULL) { printf("Read word failed\n"); exit(1); } printf("fortnite"); int a = 0; //占位变量 int b = 0; int c = 0; int v = 0; int g = 0; int hit = 0; srand(time(NULL)); //用于单词选择的随机变量 int r = rand() % 3; int chances = 10; char *word = wordList[r]; //从line.txt中获取单词地址 char guess; char misses[10]; int lettercount = 0; //通过读取随机选中的单词生成空白变量 for (size_t a = 0; word[a] != 0; a++) { lettercount++; } char space[lettercount]; //在临时数组中用下划线替换空格 for (size_t c = 0; word[c] != '\0'; c++) { space[c] = 95; } char blank[lettercount * 2]; //与数组大小相等的空数组 //借鉴我的spacename函数,但现在空白输入中似乎出现了异常 int t = 0; int k = 0; while (space[t] != '\0') { k = 2 * t; if (k > lettercount * 2 - 2) { blank[k] = space[t]; break; } blank[k] = space[t]; blank[k + 1] = ' '; } while (chances > 0) { printf("Chances:%d\n", chances); printf("Misses:%d\n", misses); printf("Word:%s\n", blank); printf("Guess[Q]:"); scanf("%c\n", &guess); while (word[b] != '\0') { if (guess = 'Q') { exit(0); } if (guess = word[b]) { v = b * 2; blank[v] = guess; b++; hit = 1; } b++; } if (hit != 1) { misses[g] = guess; } if (hit = 1) { hit = 0; } chances--; g++; } }
核心问题与修复方案:
- 语法错误导致编译异常:第一个
printf中的换行符写成了换行而非\n,直接触发语法错误,需改为printf("You need to provide the word list file name.\n");。 - scanf输入阻塞:
scanf("%c\n", &guess);中的\n会让程序等待额外空白字符,改为scanf(" %c", &guess);(前置空格跳过换行、空格等空白),避免输入卡住。 - 赋值与比较混淆:多处用
=(赋值)代替==(比较),比如if (guess = 'Q')会直接把'Q'赋值给guess,导致逻辑完全错误,所有判断处需替换为==。 - 字符串未加终止符:
space数组未添加\0,导致while (space[t] != '\0')循环越界,填充后需加space[lettercount] = '\0';。blank数组未加\0,printf输出时会读取乱码,构造完成后需补充终止符。misses数组未初始化,且printf("Misses:%d\n", misses);用错格式符,字符数组应使用%s,同时要确保每次添加字符后补\0。
- 循环变量未重置:内层
while (word[b] != '\0')循环后,b未重置为0,导致后续循环无法遍历单词,需在每次猜字母后将b置0。 - 随机数越界风险:
rand() % 3固定取0-2索引,但单词列表长度由count决定,应改为rand() % count避免访问非法内存。 - 缺失必要头文件:代码使用了
printf、exit、srand等函数,需在开头添加:
否则会出现函数未定义的异常。#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h>
修改后的关键修复版本:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> char **readWordList(char *, int *); int main(int argc, char *argv[]) { char **wordList; int count = 0; if (argc != 2) { printf("You need to provide the word list file name.\n"); printf("Usage: %s filename\n", argv[0]); return -1; } wordList = readWordList(argv[1], &count); if (wordList == NULL) { printf("Read word failed\n"); exit(1); } int b = 0; int g = 0; int hit = 0; srand(time(NULL)); int r = rand() % count; int chances = 10; char *word = wordList[r]; char guess; char misses[11] = {0}; int lettercount = strlen(word); char space[lettercount + 1]; for (int c = 0; word[c] != '\0'; c++) { space[c] = '_'; } space[lettercount] = '\0'; char blank[lettercount * 2]; int t = 0; int k = 0; while (space[t] != '\0') { k = 2 * t; blank[k] = space[t]; if (k < lettercount * 2 - 1) { blank[k + 1] = ' '; } t++; } blank[lettercount * 2 - 1] = '\0'; while (chances > 0) { printf("Chances:%d\n", chances); printf("Misses:%s\n", misses); printf("Word:%s\n", blank); printf("Guess[Q]:"); scanf(" %c", &guess); if (guess == 'Q' || guess == 'q') { exit(0); } hit = 0; b = 0; while (word[b] != '\0') { if (guess == word[b]) { int v = b * 2; blank[v] = guess; hit = 1; } b++; } if (!hit && g < 10) { misses[g] = guess; g++; } chances--; } printf("Game Over! The word was: %s\n", word); return 0; }
内容的提问来源于stack exchange,提问作者black_bleu
相关产品推荐
相关产品推荐

