正确的循环逻辑?

时间:2015-11-19 23:35:01

标签: c++ c string algorithm c++11

我的刽子手程序出了问题。我需要为玩家所做的每次猜测显示板,如果答案是正确的,则板不会改变。如果答案不正确,董事会将改变。我尝试进行循环,以便当用户进行6次错误猜测时,将显示最终的棋盘,这意味着玩家输掉了游戏。到目前为止,这是我的代码......

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

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; 
ifstream inFile;
int wrongGuess = 0;
char letterGuess = 0;



// while loop to get the last word from file
inFile.open("hangman.dat");
while (!inFile.eof())
{
    cout << fileWord << endl;
    inFile >> fileWord;
}

inFile.close();

//capitalize the word to use in the game of hangman
std::transform(fileWord.begin(), fileWord.end(), fileWord.begin(), toupper);
cout << "\nWord to Guess: " << fileWord << 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 < fileWord.length(); i++)
    {
        if (fileWord[i] == letterGuess)
        {
            cout << "\n" << letterGuess << " is in the letter to guess." << endl;
            found = true;
        }

    }
    // 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 << boardOne << endl;
        if (wrongGuess = 1)
            cout << boardTwo << endl;
        if (wrongGuess = 2)
            cout << boardThree << endl;
        if (wrongGuess = 3)
            cout << boardFour << endl;
        if (wrongGuess = 4)
            cout << boardFive << endl;
        if (wrongGuess = 5)
            cout << boardSix << endl;
        if (wrongGuess = 6)
            cout << boardSeven << endl;
    }
}


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

1 个答案:

答案 0 :(得分:1)

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

所有这些都是错的,你在你的条件下做作业。 作业的价值

wrongGuess = 3

是3.因此,除

外,他们都将评估为真
wrongGuess = 0