C++ C++;刽子手代码赢得';我不能正常工作
我的代码可以编译,但它不允许用户正确猜测单词,当它显示正确的单词时,它只是一串废话C++ C++;刽子手代码赢得';我不能正常工作,c++,C++,我的代码可以编译,但它不允许用户正确猜测单词,当它显示正确的单词时,它只是一串废话 #include <iostream> #include <fstream> #include <string> #include <ctime> #include <cstdlib> using namespace std; int instructions(); void manual(); void file(char*); int l
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int instructions();
void manual();
void file(char*);
int letterFill(char, char*, char*);
void Unknown(char*, char*);
const int MAX_LENGTH = 10;
void main()
{
bool done = false;
char word[MAX_LENGTH];
char unknown[MAX_LENGTH];
char letter;
char name[MAX_LENGTH];
int wrong_guesses = 0;
int MAX_TRIES;
char ans;
while (!done)
{
switch (instructions())
{
case 1:
{
manual();
break;
}
case 2:
{
file(word);
break;
}
}
cout << "WHAT IS THE NUMBER OF GUESSES ALLOWED?: " << endl;
cin >> MAX_TRIES;
Unknown(word, unknown);
cout << endl << endl << "HANGMAN";
cout << endl << endl << "Each letter is represented by a star." << endl;
cout << "You have " << MAX_TRIES << " tries left.";
cout << "ENTER GUESS WHEN READY: ";
cin >> letter;
while (letter != 'N' && letter != 'n')
{
while (wrong_guesses < MAX_TRIES)
{
cout << unknown << endl;
cout << "Guess a letter: " << flush;
cin >> letter;
if (letterFill(letter, word, unknown) == 0)
{
cout << endl << "You got it wrong! You lose a guess" << endl;
wrong_guesses++;
}
else
{
cout << endl << "Pfft, you got lucky" << endl;
}
cout << "Guesses Left: " << MAX_TRIES - wrong_guesses << endl;
if (strcmp(word, unknown) == 0)
{
cout << word << endl;
cout << "You got it!" << endl;
exit(0);
}
cout << "You've been hanged." << endl;
cout << "The word was : " << word << endl;
}
}
cout << "Try again? (y/n): " << flush;
cin >> ans;
if (ans == 'y' || ans == 'Y')
done = true;
else
done = false;
}
system("pause");
}
int instructions()
{
int select = 0;
cout << endl << "HANGMAN" << endl << endl;
cout << " PROGRAM MENU" << endl;
cout << " Select option 1 or 2" << endl << endl;
cout << " 1. INPUT WORD MANUALLY" << endl;
cout << " 2. PLAY AGAINST THE COMPUTER" << endl;
cout << " 3. EXIT PROGRAM BY INPUTING: N or n" << endl << endl;
cin >> select;
return select;
}
void manual()
{
string word;
cout << endl << "INPUT WORD: " << endl;
cin >> word;
return;
}
void file(char *roc)
{
ifstream fin("word.txt");
int x;
int count = 1;
int word;
int i = 0;
srand(time(0));
word = rand() % 20;
while (count < word)
{
fin >> x;
if (x == 0)
{
count++;
}
}
do
{
fin >> x;
roc[i++] = char(x);
} while (x);
return;
}
int letterFill(char guess, char *secretword, char *guessword)
{
int i;
int matches = 0;
for (i = 0; i<MAX_LENGTH; i++)
{
if (secretword[i] == 0)
{
break;
}
if (guess == guessword[i])
{
return 0;
}
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}
void Unknown(char *word, char *unknown)
{
int i;
int length = strlen(word);
for (i = 0; i<length; i++)
{
unknown[i] = '*';
}
unknown[i] = 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int指令();
作废手册();
无效文件(字符*);
整型字母填充(字符,字符*,字符*);
无效未知(字符*,字符*);
const int MAX_LENGTH=10;
void main()
{
bool done=false;
字符字[最大长度];
字符未知[最大长度];
字符字母;
字符名称[最大长度];
int错误猜测=0;
int MAX_尝试;
查尔安斯;
而(!完成)
{
开关(指令())
{
案例1:
{
手动();
打破
}
案例2:
{
文件(word);
打破
}
}
不能最大限度地尝试;
未知(单词,未知);
当你阅读手册()中的一个单词时您没有将其存储在任何有用的位置。当函数返回时,您读取的“字符串字”将丢失。此外,我将在将来改进您的变量命名。您多次使用word变量,其中word是char*、char array、int和std::string。垃圾来自使用char数组而不初始化它们。我将使用std::stri而不是字符数组。