【问题标题】:how to compare string word to chat guess?如何将字符串单词与聊天猜测进行比较?
【发布时间】:2014-12-07 15:21:26
【问题描述】:

我目前正在开发一个程序(hangman),但在将用户输入(char)与要猜测的单词(string)进行比较以确定猜测的字母是否在单词中时遇到问题。

#include <iostream>
#include <fstream>  // ifstream and ofstream
#include <iomanip>  // input/output manipulation
#include <cctype>   // toupper() function
#include <cstring>
#include <string>   // strings
using namespace std;
#include "MyFuncts.h" // programmer defined includes

//ASCII ART FROM: http://ascii.co.uk/art/hangman by Manus O'Donnell

int main()
{
    ifstream inFile;  // used to read from the file
    string stage0;
    string stage1;
    string stage2;
    string stage3;
    string stage4;
    string stage5;
    string stage6;
    string word; // convert this to array down the road
    char guess;
    int numWrong = 0;
    bool found = true;

    //STAGE0
    stage0 = " ___________.._______\n| .__________))______|\n| | / /      ||\n| |/ /       ||\n| | /        ||\n| |/\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n\"\"\"\"\"\"\"\"\"\"|_        |\"\"\"|\n|\"|\"\"\"\"\"\"\"\ \       '\"|\"|\n| |        \\ \\        | |\n: :         \\ \\       : :\n. .          `'       . .\n\n\n";
    //STAGE1
    stage1 = " ___________.._______\n| .__________))______|\n| | / /      ||\n| |/ /       ||\n| | /        ||.-''.\n| |/         |/  _  \\\n| |          ||  `/,|\n| |          (\\\\`_.'\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n\"\"\"\"\"\"\"\"\"\"|_        |\"\"\"|\n|\"|\"\"\"\"\"\"\"\ \       '\"|\"|\n| |        \\ \\        | |\n: :         \\ \\       : :\n. .          `'       . .\n\n\n";
    //STAGE2
    stage2 = " ___________.._______\n| .__________))______|\n| | / /      ||\n| |/ /       ||\n| | /        ||.-''.\n| |/         |/  _  \\\n| |          ||  `/,|\n| |          (\\\\`_.'\n| |         .-`--'.\n| |        /Y . . Y\\\n| |          |   |\n| |          | . |\n| |          |   |\n| |\n| |\n| |\n| |\n| |\n\"\"\"\"\"\"\"\"\"\"|_        |\"\"\"|\n|\"|\"\"\"\"\"\"\"\ \       '\"|\"|\n| |        \\ \\        | |\n: :         \\ \\       : :\n. .          `'       . .\n\n\n";
    //STAGE3
    stage3 = " ___________.._______\n| .__________))______|\n| | / /      ||\n| |/ /       ||\n| | /        ||.-''.\n| |/         |/  _  \\\n| |          ||  `/,|\n| |          (\\\\`_.'\n| |         .-`--'.\n| |        /Y . . Y\\\n| |       // |   |\n| |      //  | . |\n| |     ')   |   |\n| |\n| |\n| |\n| |\n| |\n\"\"\"\"\"\"\"\"\"\"|_        |\"\"\"|\n|\"|\"\"\"\"\"\"\"\ \       '\"|\"|\n| |        \\ \\        | |\n: :         \\ \\       : :\n. .          `'       . .\n\n\n";
    //STAGE4
    stage4 = " ___________.._______\n| .__________))______|\n| | / /      ||\n| |/ /       ||\n| | /        ||.-''.\n| |/         |/  _  \\\n| |          ||  `/,|\n| |          (\\\\`_.'\n| |         .-`--'.\n| |        /Y . . Y\\\n| |       // |   | \\\\\n| |      //  | . |  \\\\\n| |     ')   |   |   (`\n| |\n| |\n| |\n| |\n| |\n\"\"\"\"\"\"\"\"\"\"|_        |\"\"\"|\n|\"|\"\"\"\"\"\"\"\ \       '\"|\"|\n| |        \\ \\        | |\n: :         \\ \\       : :\n. .          `'       . .\n\n\n";
    //STAGE5
    stage5 = " ___________.._______\n| .__________))______|\n| | / /      ||\n| |/ /       ||\n| | /        ||.-''.\n| |/         |/  _  \\\n| |          ||  `/,|\n| |          (\\\\`_.'\n| |         .-`--'.\n| |        /Y . . Y\\\n| |       // |   | \\\\\n| |      //  | . |  \\\\\n| |     ')   |   |   (`\n| |          ||'\n| |          ||\n| |          ||\n| |          ||\n| |         / |\n\"\"\"\"\"\"\"\"\"\"|_`-'     |\"\"\"|\n|\"|\"\"\"\"\"\"\"\ \       '\"|\"|\n| |        \\ \\        | |\n: :         \\ \\       : :\n. .          `'       . .\n\n\n";
    //STAGE6 - GAME OVER
    stage6 = " ___________.._______\n| .__________))______|\n| | / /      ||\n| |/ /       ||\n| | /        ||.-''.\n| |/         |/  _  \\\n| |          ||  `/,|\n| |          (\\\\`_.'\n| |         .-`--'.\n| |        /Y . . Y\\\n| |       // |   | \\\\\n| |      //  | . |  \\\\\n| |     ')   |   |   (`\n| |          ||'||\n| |          || ||\n| |          || ||\n| |          || ||\n| |         / | | \\\n\"\"\"\"\"\"\"\"\"\"|_`-' `-' |\"\"\"|\n|\"|\"\"\"\"\"\"\"\ \       '\"|\"|\n| |        \\ \\        | |\n: :         \\ \\       : :\n. .          `'       . .\n\n\n";

    inFile.open("hangman.dat");
    if (!inFile)
    {
        cout << "Error opening file for reading\n";
        system("pause");
        return 1;
    }// end if
    inFile >> word; // convert these into an array as well
    while (!inFile.fail()) // .fail is better than .eof as it catches more issues
    {
        cout << "Word: " << word << endl;;
        inFile >> word;
    }
    inFile.close();

    word = caseChanger(word, true);

    //INPUT
    cout << "Word to Guess: " << word << endl << endl;

    //PROCESS & OUTPUT
    while (numWrong <= 6)
    {
        if (numWrong == 0)
            cout << stage0 << endl;
        else if (numWrong == 1)
            cout << stage1 << endl;
        else if (numWrong == 2)
            cout << stage2 << endl;
        else if (numWrong == 3)
            cout << stage3 << endl;
        else if (numWrong == 4)
            cout << stage4 << endl;
        else if (numWrong == 5)
            cout << stage5 << endl;
        else
            cout << stage6 << endl;

        cout << "Please enter a letter to guess: ";
        cin >> guess;
        guess = toupper(guess);
        cout << "You entered: " << guess << endl;

        for(int i = 0; i < word.length(); i++)
        {
            if(word[i] == guess)
                found = true;
            else
                found = false;
        }

        if (found)
        {
            cout << guess << " is in the word to guess." << endl;
        }
        else
        {
            cout << guess << " is NOT in the word to guess." << endl;
            numWrong = numWrong++;
        }
    }

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

由于某种原因,当我输入一个作为单词一部分的字母时,这仍然表明它不是,并增加了错误猜测的数量 (numWrong)。

可以使用新的眼睛,因为我不知道为什么这目前不起作用。

谢谢!

【问题讨论】:

  • caseChanger() 是做什么的?
  • caseChanger 是一个用户定义的函数,它接受一个字符串变量并更改 ketter 的大小写(如果为 true,则转换为大写,如果为 false,则转换为小写)。
  • 请把你的阶段保持在一个数组中,这样你就可以编写循环而不是 6 if elses!

标签: c++


【解决方案1】:

问题在于您的循环以确定字母是否正确。如果找到正确的字母,则循环不会结束,而是继续遍历所有字母,这会将其更改回 false。

found = false; //Move this here to set it as false default before the loop starts
for(int i = 0; i < word.length(); i++)
    {
        if(word[i] == guess)
        {
            found = true;
            break; //Add this here to exit the loop if letter is found
        }
    }

【讨论】:

  • 一旦你完成了你的操作,当它为真时,只需让 found = false。 if (found) { cout
【解决方案2】:

当你完成你的操作后,只要found = false是真的。

if (found)
{
    cout << guess << " is in the word to guess." << endl;
    found = false; // Get it reset for the next loop.
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    • 1970-01-01
    相关资源
    最近更新 更多