2014-10-21 3 views
1

작은 추측 게임을 거의 끝내지 만 해결 방법을 모르는 문제가 발생했습니다.C++ 함수가 호출되지 않을 때 호출되는 함수

문제가 있는지 추측되는 입력이 1 ~ 100

프로그램을 처음 실행, 모든 것이 잘 작동 숫자입니다 수 있도록 확인하는 check_guess 기능입니다.

http://i.imgur.com/pprunDT.png (내 명성이 너무 낮은 아니었다면 나는 이미지를 게시 할 것)

그러나 이후 때마다, 그래이 선택되어 다시 재생하는 경우, 프로그램은 check_guess 기능과 표시 "잘못된 입력"을 통해 실행 그것은

http://i.imgur.com/8OSnSJt.png

안 나는 프로그램이 동작 방법을 모르겠어요.

전체 프로그램에 대한 코드는 여기에 있습니다 :

#include <iostream> 
#include <cstdlib>  //for rand 
#include <ctime>   //for time 
#include <string> 
#include <sstream>  //for conversions from string to int 

using namespace std; 

int check_guess(int tries) { //function for limiting the input of guess 
    string guess = ""; 
    int result = 0; 

    do { 
     getline (cin, guess); 

     istringstream convert(guess); 
     if (!(convert >> result) || (result < 1 || result > 100)) { 
      result = 0; 
      cout << "Invalid Input.\n" << endl; 
      cout << "You have " << tries << " tries: "; 
     } 
    } while (result == 0); 

    return result; 
} 

bool play_again() { //function for limiting the input of mode 
    bool quit; 
    string yn; 
    do { 
     cin >> yn; 
     if   (yn == "y" || yn == "yes") { 
         quit = false; 
     } 
     else if (yn == "n" || yn == "no") { 
         quit = true; 
     } 
     else { 
      yn = "invalid"; 
      cout << "Invalid input.\n\nEnter 'y' or 'n': "; 
     } 
    } while (yn == "invalid"); 
    return quit; 
} 

int main() 
{ 
    srand(time(0));   //sets seed to be random 
    int mystery = 0;  //defines mystery number 
    int guess = 0;   //defines guess 
    int tries = 5;   //defines trys 
    bool quit = false; //defines replay or quit 

    cout << "----------------------------------\n"; 

    do {            //while mode is not set to quit, keep  playing 
     tries = 5;         //resets tries each new game 
     mystery = rand() % 100 + 1; //sets mystery number to be random 
     guess = 0; 
     cout << "Pick a number between 1 and 100.\n\nYou have 5 tries: "; 

     while (tries != 0) {    //loops until you have no tries left 
      guess = check_guess(tries); 

      if (guess == mystery) { tries = 0; } //if you guess right it ends the loop 
      else         { tries--; }  //guessing wrong lowers tries by 1 

      if (tries != 0 && guess > mystery) { 
       cout << guess << " is too high.\n" << endl; 
       cout << "You have " << tries << " tries: "; 
      } 
      if (tries != 0 && guess < mystery) { 
       cout << guess << " is too low.\n" << endl; 
       cout << "You have " << tries << " tries: "; 
      } 
     } 


     if (guess == mystery) {  //if guess == mystery by time loop ends you win 
      cout << "Got it! You Win!\n" << endl; 
     } 
     else {          //if not, you lose 
      cout << "You Lose! The number was: " << mystery << ".\n" <<endl; 
     } 

     cout << "-------------------\n"; 
     cout << "Play Again?(y/n): "; //ask user to play again 
     quit = play_again(); 
     cout << "-------------------\n"; 
     if (quit == false) 
      cout << endl; 
    } while (quit == false); 

    cout << "----------------------------------" << endl; 
    return 0; 
} 

나는이 문제를 해결하는 방법을 모르겠어요.

+0

당신은 아마보기 위해 몇 가지 간단한 디버깅을해야 할 경우''경우 ((||) >> (결과를 결과를 변환 <1 개 || 결과에 조건! > 100)''성명서는 당신이 기대하는 것입니다. 그것은 제가 예상 한 바로 그 문제를 안내해 줄 것입니다. – aruisdante

+0

"Play Again Again?"에서 'y'대답 후 'end of line'이 아닙니다. 첫 번째로 숫자가 –

답변

2

이 라인 :

cin >> yn; 

만 'Y'가 아닌 라인의 끝을 읽습니다. 결과적으로이 명령의 다음 실행

getline (cin, guess); 

은 공백 문자열로 추측을 초기화합니다.

+0

이라고 생각되면 빈 줄이 생깁니다. 해결책을 제안하십시오. –

+0

줄의 끝을 명시 적으로 버리거나 'getline (cin, yn); ' –

+0

또한'cin.ignore() '. –

0

온라인 19에서 "cin.ignore();"코드를 가져옵니다. 인용없이. 코드가 다음과 같이 표시됩니다

int check_guess (int tries) {// 추측 입력을 제한하는 기능 string guess = ""; int result = 0;

do { 
    getline (cin, guess); 

    istringstream convert(guess); 
    if (!(convert >> result) || (result < 1 || result > 100)) { 
     result = 0; 
     cin.ignore(); 
     cout << "Invalid Input.\n" << endl; 
     cout << "You have " << tries << " tries: "; 
    } 
} while (result == 0); 

return result; 

} 등등 `

합니다. 이렇게하면 콘솔에 대한 입력이 잠시 중단됩니다. 코드를 다시 읽었을 때 'y'를 읽으면 숫자 입력으로 다시 시도합니다. 작은 줄을 넣으면 cin.ignore()는 y를 두 번 입력하는 것을 멈 춥니 다.

0

변경 play_again (일) :

bool play_again() { //function for limiting the input of mode 
    bool quit; 
    string yn; 
    do { 
     getline (cin, yn); 
     if   (yn == "y" || yn == "yes") { 
         quit = false; 
     } 
     else if (yn == "n" || yn == "no") { 
         quit = true; 
     } 
     else { 
      yn = "invalid"; 
      cout << "Invalid input.\n\nEnter 'y' or 'n': "; 
     } 
    } while (yn == "invalid"); 
    return quit; 
} 
관련 문제