C++ 清除输入缓冲区

C++ 清除输入缓冲区,c++,C++,我在写一个程序,这个程序基本上是一个猜谜游戏。计算机显示一个数字,用户必须猜测他们的数字是高、低还是正确。我已经制作了这个程序,它非常棒,但唯一不棒的是,当用户决定再次玩游戏时,我不知道如何去掉输入缓冲区。每次用户想要玩游戏时,游戏都会重新开始,但输入与上一个游戏相同。我试着把cin.clear()放在我能想到的任何地方,也放在cin.clear()上。但这似乎不起作用。如何清除输入 #include <iostream> using namespace std; int mai

我在写一个程序,这个程序基本上是一个猜谜游戏。计算机显示一个数字,用户必须猜测他们的数字是高、低还是正确。我已经制作了这个程序,它非常棒,但唯一不棒的是,当用户决定再次玩游戏时,我不知道如何去掉输入缓冲区。每次用户想要玩游戏时,游戏都会重新开始,但输入与上一个游戏相同。我试着把cin.clear()放在我能想到的任何地方,也放在cin.clear()上。但这似乎不起作用。如何清除输入

#include <iostream>

using namespace std;

int main ()

{
    
int num1 = 100;
    
    char choice;
    
    num1 = num1 / 2;
    
    do 
    {
        cout << "My guess is " << num1 << ". " << "Enter 'l' if your number is lower, 'h' if it is higher, 'c' if it is correct: ";
        cin >> choice;
        cin.clear();
        
        if (choice == 'h')
        {
            num1 = num1 + 100;
            num1 = num1 / 2;
        }
        
        if (choice == 'l')
        {
            num1 = num1 + num1;
            num1 = num1 - 11;
            num1 = num1 / 2;
        }

        if (choice == 'c')
        {
            cout << "Great! Do you want to play again (y/n)?: ";
            cin >> choice;
        }
    } while (choice != 'c' || choice == 'Y' || choice == 'y' || choice == 'n' || choice == 'N');
    
    return 0;
}
#包括
使用名称空间std;
int main()
{
int num1=100;
字符选择;
num1=num1/2;
做
{

cout要重新启动游戏,您需要重置
num1
。将初始值放入一个不更改的变量中

    const int init = 100;
    char choice;
    int num1 = init / 2;
当计算机正确猜测时:

        if (choice == 'c')
        {
            num1 = init / 2;   // reset
            cout << "Great! Do you want to play again (y/n)?: ";
            cin >> choice;
        }
<>你也应该研究分而治之算法。为了使计算机有效,它应该总是在可能的范围内进行猜测,而这不是它现在正在做的。它甚至在既定范围之外跳上跳下。另一种方法是保持两个变量能够缩小PO。你也可以做两个独立的循环,一个内循环用来猜测数字,另一个外循环只询问用户是否想再次玩

例如:

#include <iostream>

int main() {
    const int initlo = 1;
    const int inithi = 100;

    char choice;
    do {
        std::cout << "Think of a number [" << initlo << "," << inithi << "]\n";
        int numlo = initlo; // initialize the range
        int numhi = inithi;
        int guess;
        do {
            guess = (numlo + numhi) / 2;  // guess in the middle of the  range

            std::cout
                << "My guess is " << guess << ". "
                << "Enter 'l' if your number is lower, 'h' if it is higher, 'c' "
                   "if it is correct: ";

            std::cin >> choice;

            if(choice == 'h')      // must be in the range (guess,numhi]
                numlo = guess + 1;
            else if(choice == 'l') // must be in the range [numlo,guess)
                numhi = guess - 1;

            // exit the loop if the user cheats or the answer is correct
        } while(numlo <= numhi && choice != 'c');

        if(choice == 'c') std::cout << "Great! ";
        else              std::cout << "Cheater! ";

        std::cout << "Do you want to play again (y/n)?: ";
        std::cin >> choice;
    } while(choice == 'Y' || choice == 'y');

    std::cout << "Bye\n";
}
#包括
int main(){
常数int initlo=1;
常数int inithi=100;
字符选择;
做{

std::你的意思是说当我尝试输入h后我的程序立即退出时,出于某种原因,&&&&&&&
不是
| |
。@πάνταῥεῖwhile条件完全错误(如果
choice==“y”
为真,则
choice!=“c”
也将为真)。当您需要循环以重新启动游戏时,您不会做任何更改游戏状态的操作。您需要添加代码以将状态重置回正确的“游戏开始”状态。请尝试
切换(选项)
.IMHO,
开关
语句更易于阅读,便于选择或菜单处理。
#include <iostream>

int main() {
    const int initlo = 1;
    const int inithi = 100;

    char choice;
    do {
        std::cout << "Think of a number [" << initlo << "," << inithi << "]\n";
        int numlo = initlo; // initialize the range
        int numhi = inithi;
        int guess;
        do {
            guess = (numlo + numhi) / 2;  // guess in the middle of the  range

            std::cout
                << "My guess is " << guess << ". "
                << "Enter 'l' if your number is lower, 'h' if it is higher, 'c' "
                   "if it is correct: ";

            std::cin >> choice;

            if(choice == 'h')      // must be in the range (guess,numhi]
                numlo = guess + 1;
            else if(choice == 'l') // must be in the range [numlo,guess)
                numhi = guess - 1;

            // exit the loop if the user cheats or the answer is correct
        } while(numlo <= numhi && choice != 'c');

        if(choice == 'c') std::cout << "Great! ";
        else              std::cout << "Cheater! ";

        std::cout << "Do you want to play again (y/n)?: ";
        std::cin >> choice;
    } while(choice == 'Y' || choice == 'y');

    std::cout << "Bye\n";
}