2014-10-13 36 views
0

我目前正在研究一個程序(hangman),並且在比較用戶輸入(字符)和要猜測的單詞(字符串)以確定猜測的字母是否在單詞中時遇到問題。如何比較字符串與聊天猜測?

#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)的數量。

可以使用重新設置的眼睛,因爲我不知道爲什麼這不是目前正在工作。

謝謝!

+0

'caseChanger()'做了什麼? – Shadow

+0

caseChanger是一個用戶定義的函數,它接受一個字符串變量並更改大小寫(如果爲true,將大小寫轉換爲大寫,如果爲false,則轉換爲小寫)。 – Meta

+0

請保持你的階段在一個數組中,所以你可以寫循環而不是6,如果elses! – zoska

回答

2

問題出在您的循環中以確定字母是否正確。如果找到正確的字母,則循環不會結束,並繼續檢查所有字母,然後將其更改爲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 
     } 
    } 
+0

只要在您的操作完成後立即找到= false就行了。 if(found) cout << guess <<「是在猜詞中。」 << endl; found = false; //將它重置爲下一個循環。 } – fox

1

只要在您的操作完成後立即創建found = false

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