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

C++ Function being called when it isn't supposed to

本文关键字:调用 函数 不应该 C++      更新时间:2023-10-16

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

问题出在check_guess函数上,该函数正在检查以确保输入的猜测是一个介于1和100之间的数字。

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

https://i.stack.imgur.com/644yd.png(如果我的声誉不那么低,我会发布图片)

但每次选择"是"再次播放后,程序都会运行check_guess函数,并在不应该时显示"无效输入"

https://i.stack.imgur.com/E4rpb.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.nnEnter '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.nnYou 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;
}

我不知道该怎么解决。

此行:

cin >> yn;

只读取"y"而不读取行的末尾。因此,该指令的下一次执行

getline (cin, guess);

将猜测初始化为空字符串。

在第19行,导入不带引号的代码"cin.ignore();"。所以你的代码读起来是

`int check_guess(int trys){//用于限制猜测输入的函数string猜测=";int结果=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。

将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.nnEnter 'y' or 'n': ";
        }
    } while ( yn == "invalid" );
    return quit;
}