C ++函数在不应该被调用时被调用

时间:2014-10-21 18:24:11

标签: c++ function

我几乎完成了一个小型猜谜游戏,但我遇到了一个我不知道如何解决的问题。

问题在于check_guess函数正在检查以确保输入的猜测是1到100之间的数字。

第一次运行程序时,一切正常。

http://i.imgur.com/pprunDT.png(如果我的名声不低,我会张贴图片)

但每次之后,如果再次选择是,则程序会运行check_guess函数并显示" Invalid Input"什么时候不应该

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;
}

我不确定如何解决这个问题。

3 个答案:

答案 0 :(得分:2)

这一行:

cin >> yn;

只读取'y'而不是行尾。结果,下一次执行该指令

getline (cin, guess);

将guess初始化为空字符串。

答案 1 :(得分:0)

在第19行,输入代码&#34; cin.ignore();&#34;没有引用。 所以你的代码读作

`int check_guess(int tries){//用于限制猜测输入的函数     string guess =&#34;&#34 ;;     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;

} `

等等。这会暂时停止输入控制台。您的代码正在阅读&#39; y&#39;重新启动时再次尝试作为数字的输入。放入小行cin.ignore(),阻止它输入y两次。

答案 2 :(得分: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;
}