[英]I'm trying to make a program in C in which you can play hangman but as soon as it prints the option to guess the letter the program terminates
The program isn't printing after giving me the first chance to guess.在给了我第一次猜测的机会后,程序没有打印出来。
#include <stdio.h>
#include <string.h>
int main() {
char menu;
int c = 0, flag = 0, life = 8;
printf("\nWelcome to Hangman!!!");
printf("\nThis is a game of hangman.");
printf("Player 1 enters a random word and the other has to guess it.");
printf("You get 8 lives in total i.e. you can have a maximum of 8 wrong guesses.");
printf("\n");
printf("Press n for new game\n");
printf("Press q to quit\n");
printf("\n");
scanf("%c", &menu);
int i = 0, j = 0;
char w[20], ch;
if (menu == 'q') {
printf("Exiting...");
printf("Thanks for playing");
}
else if (menu == 'n') {
printf("Player 1 enters a word\n");
scanf("%s", w);
int len = strlen(w);
for (int i = 0; i < len; i++) {
toupper(w[i]);
}
printf("\e[1;1H\e[2J");
char arr[len - 1];
for (int i = 0; i < len - 1; i++) {
arr[i] = '_';
printf("%c", arr[i]);
}
printf("\n");
while (life != 0) {
for (int i = 0; i < len - 1; i++) {
if (arr[i] == '_') {
flag = 1;
break;
}
else {
flag = 0;
}
}
if (flag == 0) {
printf("You Won!!\n");
printf("You Guessed The Word: %s", w);
break;
}
else {
char ans;
printf("Enter a letter between A-Z");
scanf("%c", ans);
toupper(ans);
for (int j = 0; j < len; j++) {
if (ans == w[j]) {
arr[j] = ans;
c++;
}
}
if (c == 0) {
life--;
}
c = 0;
for (int j = 0; j < len; j++) {
printf("%c", arr[j]);
}
printf("\n Lives Remaining= %d \n", life);
}
}
if (life == 0) {
printf("\n You Lost!!! \n");
printf("The Word Was: %s", w);
}
}
else {
printf("Invalid Character");
}
}
Output:
Output:
Welcome to Hangman!!!欢迎来到刽子手!!!
This is a game of hangman.Player 1 enters a random word and the other has to >guess it.You get 8 lives in total ie you can have a maximum of 8 wrong >guesses.这是一个刽子手游戏。玩家 1 输入一个随机单词,其他人必须 >猜测它。你总共获得 8 个生命,即你最多可以有 8 个错误 >猜测。
Press n for new game按 n 进入新游戏
Press q to quit按q退出
n
n
Player 1 enters a word玩家 1 输入一个单词
Hello你好
Enter a letter between AZ PS C:\Users\arora\Desktop\Programs\C>在 AZ PS C:\Users\arora\Desktop\Programs\C> 之间输入一个字母
There are quite a few problems with your program.你的程序有很多问题。 Here are the major ones:
以下是主要的:
scanf(" %c", ...)
to ensure previous newlines are ignored.scanf(" %c", ...)
的格式字符串中使用空格前缀,以确保忽略以前的换行符。scanf("%c", ans);
should be &ans
.&ans
。 It causes scanf()
to fail and iterate through all guesses without additional data from user.scanf()
失败并在没有来自用户的额外数据的情况下遍历所有猜测。 It also means no input was obtained from user so comparing it against word would never work. Here are some of the other issues:以下是其他一些问题:
#include <ctype.h>
. #include <ctype.h>
。printf()
per line which makes it hard to read.printf()
,这使得它难以阅读。 Use a single call and multi-line strings as input.scanf()
.scanf()
的返回值。 If it fails then whatever variable it read doesn't have a well defined value.scanf()
read no more than 19 bytes into a 20 byte array w
.scanf()
将不超过 19 个字节读取到 20 字节数组w
中。 It takes a little macro magic to generate the 19 so I didn't make this change but it's a good idea to #define
constants for magic values like the 20.#define
常量是个好主意。arr
is not \0
terminated (len-1). arr
不是\0
终止的 (len-1)。 Most c programmers expect a string so it's not worth the confusion to save 1 byte.i
, j
.i
, j
。flag
variable is cumbersome. flag
变量的计算比较麻烦。toupper()
you can create a copy of the user input with letters in upper case, and create another variable to hold the upper case version of the player's guess.toupper()
,您可以创建一个用户输入的大写字母副本,并创建另一个变量来保存玩家猜测的大写版本。 This avoid you saying things like you entered the word "BOB" when the actual input was "bob".toupper()
to convert each letter to upper case but don't assign the result to anything so it does not do anything constructive.toupper()
将每个字母转换为大写,但不将结果分配给任何内容,因此它不会做任何建设性的事情。c
, w
, arr
, flag
). c
, w
, arr
, flag
)。#include <ctype.h>
#include <stdio.h>
#include <string.h>
#define clear() printf("\e[1;1H\e[2J")
int main() {
printf(
"Welcome to Hangman!!!\n"
"\n"
"This is a game of hangman.\n"
"Player 1 enters a random word and the other has to guess it.\n"
"You get 8 lives in total i.e. you can have a maximum of 8 wrong guesses.\n"
"\n"
"Press n for new game\n"
"Press q to quit\n"
);
char menu;
if(scanf(" %c",&menu) != 1) {
printf("scanf failed\n");
return 1;
}
switch(menu) {
case 'q':
printf(
"Exiting..."
"Thanks for playing\n"
);
return 0;
case 'n':
break;
default:
printf("Invalid Character");
return 1;
}
printf("Player 1 enters a word\n");
char w[20];
if(scanf("%19s", w) != 1) {
printf("scanf failed\n");
return 1;
}
clear();
char arr[20];
int len=strlen(w);
for(int i=0;i<len;i++) {
arr[i]='_';
}
arr[len] = '\0';
int life=8;
for(;;) {
printf("%d Lives Remaining\n", life);
// read a guess from player
for(int i = 0; i < len; i++) {
printf("%c", arr[i]);
}
printf(" Enter a letter between A-Z ");
char guess;
if(scanf(" %c", &guess) != 1) {
printf("scanf failed\n");
return 1;
}
// determine if any of the letters are in the secret word
int c = 0;
for(int i=0; i<len; i++) {
if(toupper(guess) == toupper(w[i])) {
arr[i]=guess;
c = 1;
}
}
if(c==0) {
life--;
}
// game over?
int flag = 0;
for(int i = 0; i<len; i++) {
if(arr[i]=='_') {
flag=1;
break;
}
}
if(flag==0) {
printf("You Won!!\n");
printf("You Guessed The Word: %s\n",w);
break;
}
if(life==0) {
printf("\n You Lost!!!\n");
printf("The Word Was: %s\n", w);
break;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.