2014-12-19 2 views
-2

교수형 집행 프로젝트를하려하지만 내 코드가 작동하지 않습니다. 적절한 문자를 입력 할 때마다 코드는 그것이 잘못되었다고 (올바른 경우에도) 말합니다. 왜 그런지 모르겠지만 - 코드가 어느 시점에서 작동했지만 몇 가지를 변경했는데 이제는 작동하지 않는 이유를 모르겠습니다. 그래서 아마 간단한 수정이지만, 나는 그것을 보지 않고 있습니다.C++ 행맨 게임, "올바른"문자 스틱을 만드는 방법

도움이 될 것입니다!

#include <iostream> 

using namespace std; 

int letterFill (char, string, string&); 


int main() 
{ 
    string name; 
    int maxAttempts = 5; 
    int wrongGuesses; 
    char letter; 

    srand(time(0)); 

    const string wordList[15] = { "hanukkah", "sparklers", "mistletoe", "menorah", "presents", "reindeer", 
     "kwanzaa", "snowman", "eggnog", "celebration", "yuletide", "resolution", "nutcracker", "ornaments", "gingerbread" }; 

    string correctWord = wordList[rand() % 15]; 
    string unknown(correctWord.length(),'*'); 

    cout << correctWord << endl; 

    cout << "Welcome to a fun game of winter holiday hangman! What is your name? " << endl; 
    cin >> name; 
    cout << name <<", there are some simple things you should know about this game before you start playing!" << endl; 
    cout << "You will be trying to guess a randomly selected word by typing in ONE letter at a time " << endl; 
    cout << "You will have " << maxAttempts << " tries before losing the game " << endl; 
    cout << "And remember, all of the words are winter holiday related. Good luck " << name <<"!" << endl; 
    cout << "*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*" <<endl; 

    while (wrongGuesses == 0) 
    { 
     cout << "Guess a letter" << cout; 
     cin >> letter; 

     if (letterFill(letter, correctWord, unknown)==0) 
     { 
      cout << endl << "That letter is not in this word! Try again " << endl; 
      wrongGuesses = wrongGuesses + 1; 
     } 

     else 
     { 
      cout << endl << "You found a letter! Keep up the good work! " << endl; 
     } 

     if (correctWord==unknown) 
     { 
      cout << correctWord << endl; 
      cout << "Congratulations! You guessed the correct word!" << endl; 

     } 

    } 

    while (wrongGuesses == 1) 
    { 
     cout << "You have 4 guesses left " << endl; 
     cout << "Guess a letter " << cout; 
     cin >> letter; 

     if (letterFill(letter, correctWord, unknown)==0) 
     { 
      cout << endl << "That letter is not in this word! Try again " << endl; 
      wrongGuesses = wrongGuesses + 1; 
     } 
     else 
     { 
      cout << endl << "You found a letter! Keep up the good work! " << endl; 
     } 

     if (correctWord==unknown) 
     { 
      cout << correctWord << endl; 
      cout << "Congratulations! You guessed the correct word!" << endl; 

     } 
    } 

    while (wrongGuesses == 2) 
    { 
     cout << "You have 3 guesses left " << endl; 
     cout << "Guess a letter " << cout; 
     cin >> letter; 

     if (letterFill(letter, correctWord, unknown)==0) 
     { 
      cout << endl << "That letter is not in this word! Try again " << endl; 
      wrongGuesses = wrongGuesses + 1; 
     } 
     else 
     { 
      cout << endl << "You found a letter! Keep up the good work! " << endl; 
     } 

     if (correctWord==unknown) 
     { 
      cout << correctWord << endl; 
      cout << "Congratulations! You guessed the correct word!" << endl; 

     } 
    } 

    while (wrongGuesses == 3) 
    { 
     cout << "You have 2 guesses left " << endl; 
     cout << "Guess a letter " << cout; 
     cin >> letter; 
      if (letterFill(letter, correctWord, unknown)==0) 
     { 
      cout << endl << "That letter is not in this word! Try again " << endl; 
      wrongGuesses = wrongGuesses + 1; 
     } 
     else 
     { 
      cout << endl << "You found a letter! Keep up the good work! " << endl; 
     } 

     if (correctWord==unknown) 
     { 
      cout << correctWord << endl; 
      cout << "Congratulations! You guessed the correct word!" << endl; 

     } 
    } 


    while (wrongGuesses == 4) 
    { 
     cout << "You have 1 guess left " << endl; 
     cout << "Guess a letter " << cout; 
     cin >> letter; 

      if (letterFill(letter, correctWord, unknown)==0) 
     { 
      cout << endl << "That letter is not in this word! Try again " << endl; 
      wrongGuesses = wrongGuesses + 1; 
     } 
     else 
     { 
      cout << endl << "You found a letter! Keep up the good work! " << endl; 
     } 

     if (correctWord==unknown) 
     { 
      cout << correctWord << endl; 
      cout << "Congratulations! You guessed the correct word!" << endl; 

     } 
    } 

    while (wrongGuesses == 5) 
    { 
     cout << "Sorry " << name << " you have made 5 wrong guesses!" << endl; 
     cout << "Game over. Click any key to exit. Play again soon :) " << endl; 
     if (letterFill(letter, correctWord, unknown)==0) 
     { 
      cout << endl << "That letter is not in this word! Try again " << endl; 
      wrongGuesses = wrongGuesses + 1; 
     } 
     else 
     { 
      cout << endl << "You found a letter! Keep up the good work! " << endl; 
     } 

     if (correctWord==unknown) 
     { 
      cout << correctWord << endl; 
      cout << "Congratulations! You guessed the correct word!" << endl; 

     } 
    } 

    system("pause"); 
    return 0; 
} 


int letterFill (char guessLetter, string mysteryWord, string& guessWord) 
{ 
    int x; 
    int matches=0; 
    int lengthWord=mysteryWord.length(); 
    for (x = 0; x< lengthWord; x++) 
    { 

     if (guessLetter == mysteryWord[x]) 
      return 0; 

     if (guessLetter == mysteryWord[x]) 
     { 
      guessWord[x] = guessLetter; 
      matches++; 
     } 
    } 
    return matches; 


} 
+0

'if (guessLetter == mysteryWord [x]) '줄을 확인하십시오. 테스트가 반복되고 두 번째 중첩 섹션이 실행되지 않습니다. – Sonic

답변

1

당신은 당신의 int letterFill() 함수에서 string guessWord를 업데이트하지 않습니다. 당신이 일치하는 편지를 보자 마자 두 번째로 입력하지 않고 돌아갑니다 if 성명.

나는 당신이 원하는 것은 완전히 수표를 무엇을 당신이 상대를 만나로 guessWord를 업데이트 문자열을 통해 반복되고 싶은 당신의 루프 후 그 기반으로 guessWord을 업데이트 한 후 반환하는 경우에만 가정

if(matches == 0) return 0; else return matches;

+0

진술에 대해 더 설명 할 수 있습니까? 네가 제대로 된 것 같아. –

+0

나는 그 문제를 처음에는 어디에서 던지기를 원했습니다. –

+0

_ "her"_?!? ... –