C++ c+中循环的Hangman程序+;

C++ c+中循环的Hangman程序+;,c++,loops,for-loop,while-loop,C++,Loops,For Loop,While Loop,首先,我正在使用用户定义的函数和while循环编写一个程序。我的用户定义函数getNextWord()从文件中生成一个随机单词,供我的用户用于hangman游戏。我程序的下一步是检查用户的猜测。我试图在while循环中使用while循环来实现这一点,while循环是一个for语句,它不能正常工作。我假设使用函数getNextWord()作为测试表达式是不对的。我尝试使用一个cin>>单词将生成的单词放入一个变量中,但这也不起作用。我如何编写这个循环,以便它将生成的单词中的一个字母检查为用户猜测的

首先,我正在使用用户定义的函数和while循环编写一个程序。我的用户定义函数getNextWord()从文件中生成一个随机单词,供我的用户用于hangman游戏。我程序的下一步是检查用户的猜测。我试图在while循环中使用while循环来实现这一点,while循环是一个for语句,它不能正常工作。我假设使用函数getNextWord()作为测试表达式是不对的。我尝试使用一个cin>>单词将生成的单词放入一个变量中,但这也不起作用。我如何编写这个循环,以便它将生成的单词中的一个字母检查为用户猜测的一个字母

#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
#include <algorithm> 
using namespace std;

#include "randword.h"
#include "myfuncts.h"

const string boardOne = " ------|\n |     |\n       |\n         |\n            |\n    -------\n\n";
const string boardTwo = " ------|\n |     |\n 0     |\n       |\n           |\n    -------\n\n";
const string boardThree = " ------|\n |     |\n 0     |\n |     |\n       |\n    -------\n\n";
const string boardFour = " ------|\n |     |\n 0     |\n-|     |\n       |\n    -------\n\n";
const string boardFive = " ------|\n |     |\n 0     |\n-|-    |\n       |\n    -------\n\n";
const string boardSix = " ------|\n |     |\n 0     |\n-|-    |\n  \\    |\n    -------\n\n";
const string boardSeven = " ------|\n |     |\n 0     |\n-|-    |\n/ \\    |\n    -------\n\n";

int main()
{
    // Declarations
    string fileWord; 
    int wrongGuess = 0;
    char letterGuess = 0;
    string playerYN;
    string word;

getWords("hangman.dat");

cout << "\nDo you want to play hangman? (y or n): ";
cin >> playerYN; 
PromptYN(playerYN);

getNextWord();
cout << "\n\nWord to Guess: " << getNextWord() << endl;

cout << "\n" << boardOne << endl;

while (wrongGuess != 6)
{
    cout << "\nEnter a letter to guess: ";
    cin >> letterGuess;
    letterGuess = toupper(letterGuess);
    cout << "You guessed the letter: " << letterGuess << endl;
    bool found = false;

    for (int i = 0; i < getNextWord().length(); i++)
    {
        if (getNextWord()[i] == letterGuess)
        {
            cout << "\n" << letterGuess << " is in the letter to guess." << endl;
            found = true;
            if (wrongGuess == 0)
                cout <<  "\n" << boardOne << endl;
            if (wrongGuess == 1)
                cout << "\n" << boardTwo << endl;
            if (wrongGuess == 2)
                cout << "\n" << boardThree << endl;
            if (wrongGuess == 3)
                cout << "\n" << boardFour << endl;
            if (wrongGuess == 4)
                cout << "\n" << boardFive << endl;
            if (wrongGuess == 5)
                cout << "\n" << boardSix << endl;
            if (wrongGuess == 6)
                cout << "\n" << boardSeven << endl;
        }

    }
    // if not found - increment wrong guesses
    if (!found)
    {
        wrongGuess++;
        cout << "\n" << letterGuess << " is not in the word to guess." << endl;
        //print the board that corresponds to the wrongGuess

        if (wrongGuess == 0)
            cout << "\n" << boardOne << endl;
        if (wrongGuess == 1)
            cout << "\n" << boardTwo << endl;
        if (wrongGuess == 2)
            cout << "\n" << boardThree << endl;
        if (wrongGuess == 3)
            cout << "\n" << boardFour << endl;
        if (wrongGuess == 4)
            cout << "\n" << boardFive << endl;
        if (wrongGuess == 5)
            cout << "\n" << boardSix << endl;
        if (wrongGuess == 6)
            cout << "\n" << boardSeven << endl;
    }
}


cout << "\n\n";
system("pause");
return 0;
}

注意这里发生了什么:

for (int i = 0; i < getNextWord().length(); i++)
{
    if (getNextWord()[i] == letterGuess)
离题: 此代码可以转换为从主例程调用的函数,而不是重复:

if (wrongGuess == 0)
    cout <<  "\n" << boardOne << endl;
if (wrongGuess == 1)
    cout << "\n" << boardTwo << endl;
if (wrongGuess == 2)
    cout << "\n" << boardThree << endl;
...
然后用

cout << boards[wrongGuess] << endl
考虑到这一点,我们可以写作

const string boardOne = " ------|\n"
                        " |     |\n"
                        "       |\n"
                        "         |\n"
                        "            |\n"
                        "    -------\n\n";
这样,在文本编辑器中,我们可以看到当程序输出字符串时将在控制台上显示的图片。如果有什么事 错误,它在视觉上是明显的,并且易于修复


一旦游戏开始运行,您可能需要调整棋盘的外观,这可能会浪费时间,并且会使当前的布局变得令人沮丧。

感谢您提供了一个非常值得一提的补充,@kazNo problem;谁能抵制离题,你知道吗?
const string boards[] = 
{
    " ------|\n |     |\n       |\n         |\n            |\n    -------\n\n",
    " ------|\n |     |\n 0     |\n       |\n           |\n    -------\n\n",
    " ------|\n |     |\n 0     |\n |     |\n       |\n    -------\n\n",
    " ------|\n |     |\n 0     |\n-|     |\n       |\n    -------\n\n",
    " ------|\n |     |\n 0     |\n-|-    |\n       |\n    -------\n\n",
    " ------|\n |     |\n 0     |\n-|-    |\n  \\    |\n    -------\n\n",
    " ------|\n |     |\n 0     |\n-|-    |\n/ \\    |\n    -------\n\n"
};
cout << boards[wrongGuess] << endl
const string name = "John Doe";
const string name = "John "
                    "Doe";
const string boardOne = " ------|\n"
                        " |     |\n"
                        "       |\n"
                        "         |\n"
                        "            |\n"
                        "    -------\n\n";