Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 刽子手游戏-当循环条件为“包含在数组中”时_C++_Arrays - Fatal编程技术网

C++ 刽子手游戏-当循环条件为“包含在数组中”时

C++ 刽子手游戏-当循环条件为“包含在数组中”时,c++,arrays,C++,Arrays,我正在制作一个刽子手游戏。如果用户输入的是相同的guesschar,则我在要求用户重新输入新的guesschar时遇到问题。这些guesschar包含在字母[]数组中。我从一个while循环开始,该循环要求用户重新输入guess是否与数组中存储的某个字符匹配。谢谢 void play(string word) { string copy = word; for(int i = 0; i < word.length(); i++) { copy[i]

我正在制作一个刽子手游戏。如果用户输入的是相同的guesschar,则我在要求用户重新输入新的guesschar时遇到问题。这些guesschar包含在字母[]数组中。我从一个while循环开始,该循环要求用户重新输入guess是否与数组中存储的某个字符匹配。谢谢

void play(string word)
{
    string copy = word;

    for(int i = 0; i < word.length(); i++)
    {
        copy[i] = ' ';
    }

    bool match = false;
    bool valid = false;
    int chance = 5;
    char letters[26];
    int counter = 0;
    char guess;
    while (chance > 0 && match == false)
    {
        int blanks = 0;
        for (int i = 0; i < word.length(); i++)
        {
            if(copy[i] == ' ')
                blanks++;
        }

        cout << '|';
        for(int i = 0; i < word.length(); i++)
        {
            cout << copy[i] << '|';
        }
        cout << "\t(There are '" << blanks << "' blanks)" << endl;
        cout << "Incorrect letters guessed: ";

        for (int i = 0; i<counter; i++)
        {
            cout << "'" << letters[i] << "' ";
        }

        cout << "\n\nEnter guess: ";
        cin >> guess;
        for(int i = 0; i < counter; i++)
        {
            if(guess == letters[i])
                valid = true;
        }

        while (valid == true)
        {

            for(int i = 0; i < counter; i++)
            {
                if(guess == letters[i])
                    valid = true;
                else
                    valid = false;
            }
            if (valid)
            {
                cout << "\n\nAlready tried that one. Enter guess: ";
                cin >> guess;
            }

        }
        long find = word.find(guess);

        if (find != string::npos)
        {
            copy[find] = word[find];
            for (int i = 0; i < word.length(); i++)
            {
                if (word[i] == word[find])
                {
                    copy[i] = word[find];
                }
            }
        }



        else
        {
            chance--;

            letters[counter] = guess;
            counter++;
            printMan(chance);
        }



        if (copy == word)
        {
            match = true;
        }
    }

    if (match == true)
        cout << "\nYou saved a life! You managed to win! Congrats, cheater." << endl;
    else
        cout << "\nThe man has been hung! You LOSE! The word was '" << word << "', you idiot." << endl;

}
这个for循环的问题是,在找到一个有效的猜测之后。。。你不会停止循环

继续检查字母是否也等于循环中的下一个值,以及下一个值和下一个值。valid会一次又一次地被覆盖,直到循环结束——此时valid等于最后一个字母的检查值

因此,只有当guess与字母中的最后一个字母匹配时,才会调用check-guess代码

在找到匹配的猜测时,您需要查看如何退出循环

 for(int i = 0; i < counter; i++)
        {
            if(guess == letters[i])
                valid = true;
            else
                valid = false;
        }