작은 추측 게임을 거의 끝내지 만 해결 방법을 모르는 문제가 발생했습니다.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;
}
나는이 문제를 해결하는 방법을 모르겠어요.
당신은 아마보기 위해 몇 가지 간단한 디버깅을해야 할 경우''경우 ((||) >> (결과를 결과를 변환 <1 개 || 결과에 조건! > 100)''성명서는 당신이 기대하는 것입니다. 그것은 제가 예상 한 바로 그 문제를 안내해 줄 것입니다. – aruisdante
"Play Again Again?"에서 'y'대답 후 'end of line'이 아닙니다. 첫 번째로 숫자가 –