2014-12-24 3 views
-6

C++을 처음 사용했습니다. 나는 C++에서 행맨 게임을 만드는 방법을 가르치는 온라인 코스를 따라 왔습니다. 대부분의 경우 코드가 작동하고 있었으며 디버깅 할 때마다 정상적으로 실행되었습니다. 전체 코드를 완료하고 그것을 두 번 확인했지만 오류가 발생합니다. '오류'C2181 : 일치하지 않는 불법 오류입니다.C++ 오류 C2181 : 일치하지 않는 불법 기타

여기 내 코드가 있습니다.

// Hangman game 
// CN 

// Header file 
#include "stdafx.h" 
// Input/Output 
#include <iostream> 
// Text 
#include <string> 
// Information storage 
#include <vector> 
// Figuring out the information storage 
#include <algorithm> 
// Converts lower cased letters to higher case letters 
#include <cctype> 
// Reads computers time and distributes a word from a list accordingly 
#include <ctime> 
// Library for system 
#include <cstdlib> 

using namespace std; 


int main() 
{ 
    // Constant data type which is a fixed value, MaxWrong in Pascal Text and the max number of guesses. 
    const int MaxWrong = 6; 


    vector<string> words; 
    words.push_back("SANTA CLAUSE"); 
    words.push_back("REINDEER"); 
    words.push_back("PRESENT"); 
    words.push_back("TREE"); 
    words.push_back("MILK"); 
    words.push_back("COOKIE"); 

    // Parenthesis must always match 
    srand(static_cast<unsigned int>(time(0))); 
    random_shuffle(words.begin(), words.end()); 
    const string theWord = words[0]; 
    int wrong = 0; 
    string soFar(theWord.size(), '_'); 
    string used = ""; 

    cout << "\n******** Welcome to Chaye's Hangman ********\n\n"; 
    cout << "  __ \n"; 
    cout << "  | | \n"; 
    cout << "  | \n"; 
    cout << "  | \n"; 
    cout << "  | \n"; 
    cout << "  | \n"; 
    cout << " ___|____ \n\n"; 
    cout << " Do Your Best Human \n\n"; 

    // Loop 
    while ((wrong < MaxWrong) && (soFar != theWord)) 
    { 
     cout << "\n You have " << (MaxWrong - wrong); 
     cout << " incorrect tries left.\n"; 
     cout << "\n You've used these letters:\n" << used << endl; 

     char guess; 
     cout << "\n\n Enter your guess: "; 
     cin >> guess; 
     guess = toupper(guess); 
     while (used.find(guess) != string::npos) 
     { 
      cout << "\n You've already tried " << guess << endl; 
      cout << " Enter your guess: "; 
      cin >> guess; 
      guess = toupper(guess); 
     } 

     used += guess; 

     if (theWord.find(guess) != string::npos) 
     { 
      cout << " That's right " << guess << " is in the word,\n"; 

      for (int i = 0; i < theWord.length(); ++i) 
      { 
       if (theWord[i] == guess) 
       { 
        soFar[i] = guess; 
       } 
      } 
     } 

     else 
     { 
      cout << " sorry. " << guess << " isn't in the word.\n"; 
      ++wrong; 
     } 


     // If one is wrong 
     if (wrong == 1) 
     { 
      cout << "  __ \n"; 
      cout << "  | | \n"; 
      cout << "  | O \n"; 
      cout << "  | \n"; 
      cout << "  | \n"; 
      cout << "  | \n"; 
      cout << " ___|____ \n\n"; 
      cout << " Do Your Best Human \n\n"; 
     } 
     else 
     { 
      cout << endl; 
     } 

     // If one is wrong 
     if (wrong == 2) 
     { 
      cout << "  ___ \n"; 
      cout << "  | | \n"; 
      cout << "  | O \n"; 
      cout << "  | @ \n"; 
      cout << "  | \n"; 
      cout << "  | \n"; 
      cout << " ___|____ \n\n"; 
      cout << " Do Your Best Human \n\n"; 
     } 
     else 
     { 
      cout << endl; 
     } 


     // If one is wrong 
     if (wrong == 3) 
     { 
      cout << "  ___ \n"; 
      cout << "  | | \n"; 
      cout << "  | O \n"; 
      cout << "  | /@ \n"; 
      cout << "  |  \n"; 
      cout << "  |  \n"; 
      cout << " ___|____ \n\n"; 
      cout << " Do Your Best Human \n\n"; 
     } 
     else 
     { 
      cout << endl; 
     } 

     // If one is wrong 
     if (wrong == 4) 
     { 
      cout << "  ___ \n"; 
      cout << "  | | \n"; 
      cout << "  | O \n"; 
      cout << "  | /@\ \n"; 
      cout << "  |  \n"; 
      cout << "  |  \n"; 
      cout << " ___|____ \n\n"; 
      cout << " Do Your Best Human \n\n"; 
     } 
     else 
     { 
      cout << endl; 
     } 

     // If one is wrong 
     if (wrong == 5) 
     { 
      cout << "\n You've been hung \n"; 
      cout << "  ___ \n"; 
      cout << "  | | \n"; 
      cout << "  | O \n"; 
      cout << "  | /@\ \n"; 
      cout << "  |/ \n"; 
      cout << "  |  \n"; 
      cout << " ___|____ \n\n"; 
      cout << " Do Your Best Human \n\n"; 
     } 
     else 
     { 
      cout << "\n I'm suprised you've got this far\n"; 
     } 
     cout << "\n The word was " << theWord << endl; 


    } 

    // If one is wrong 
    if (wrong == MaxWrong) 
    { 
     cout << "\n You've been hung \n"; 
     cout << "  ___ \n"; 
     cout << "  | | \n"; 
     cout << "  | O \n"; 
     cout << "  | /@\ \n"; 
     cout << "  |/\ \n"; 
     cout << "  |  \n"; 
     cout << " ___|____ \n\n"; 
     cout << " Do Your Best Human \n\n"; 
    } 
    else 
    { 
     cout << "\n I'm suprised you've got this far\n"; 
    } 
    cout << "\n The word was " << theWord << endl; 


    // Pause Console 
    system("pause"); 

    // Kills the application once done 
    return 0; 
} 
+0

나는 등, 당신이 C++에 좋은 책을 읽으십시오'C++ Primer' –

+0

주셔서 감사합니다 - 난 그 들여다 것이다! – Chaye

답변

-2

변경

** else ** 

else 

에 대한 귀하의 컴파일러는 것으로 해석된다 연산자. 그래도 실패하면 오류가있는 게시물을 수정하십시오. 또한 주목할만한 점은, -g 플래그를 사용하여 컴파일하면 어떤 디버그 출력을 얻는 것이 유익 할 수 있다는 것입니다. 문 다음과 같은 {...} 블록이 무조건 실행하면

if (theWord.find(guess) != string::npos); 

이 세미콜론 전체를 종료 :

+0

OP는 문제가있는 부분을 "표시"했다고 OP가 말했습니다. 그'**'는 실제 코드가 아닙니다. – Mat

+0

나는 오류를 받고있는 것을 보여줌으로써 사람들의 시간을 절약하고 여기에 별표를 추가했다. 나는 그것을 그냥 제거 할 것이다. – Chaye

+0

나는 그것을 읽을 때 꽤 분명하지 않다. : -/ –

1

이 라인에 세미콜론이 없어야합니다. 따라서 else 다음은 if 문과 더 이상 일치하지 않습니다.

그리고 당신은 또한 당신의 while 문에 ) 브래킷을 닫는 누락 :

while (used.find(guess) != string::npos 
{ 
... 
} 
+0

도움을 주셔서 감사합니다! 세미 콜론을 삭제했습니다. '시스템'이 모호한 오류이고 예상 된 명령문 오류가 제거되었지만 불법적 인 오류가 계속 발생합니다. – Chaye

+0

while 문에도 닫는 괄호가 없습니다. 편집을 참조하십시오. – Erbureth

+0

아, 감사합니다. 세미 콜론을 제거했을 때 실수로 괄호를 삭제했습니다. 불량합니다. 시간과 인내심을 주셔서 감사합니다. – Chaye

관련 문제