C++ 摆脱输入缓冲区

C++ 摆脱输入缓冲区,c++,C++,我制作了一个程序,根据用户的输入创建一个矩形。如果用户输入8,程序将生成一个8 x 8的矩形。我有所有的代码来制作一个矩形。但我想做的是让程序询问用户是否想要重新创建另一个矩形,它确实会这样做,但每次程序要求新的输入时,它都会显示以前创建的旧矩形。不是用户要求创建的新的。我如何摆脱以前的输入缓冲区来创建一个新的矩形?谢谢 #include <iostream> using namespace std; int main () { //declaring variable

我制作了一个程序,根据用户的输入创建一个矩形。如果用户输入8,程序将生成一个8 x 8的矩形。我有所有的代码来制作一个矩形。但我想做的是让程序询问用户是否想要重新创建另一个矩形,它确实会这样做,但每次程序要求新的输入时,它都会显示以前创建的旧矩形。不是用户要求创建的新的。我如何摆脱以前的输入缓冲区来创建一个新的矩形?谢谢

#include <iostream>

using namespace std;

int main ()

{
    //declaring variables
    int size;
    char choice;

    cout << "**************** Drawing Squares Program ******************************" << endl;
    cout << "* Algorithm generates a hollow square, using the character +, - and | *" << endl;
    cout << "* Acceptable size dimension: Any value from 3 to 20. Choose carefully.*" << endl;
    cout << "***********************************************************************" << endl;
    
    cout << "Side size = ";
    cin >> size;
    
    while (size < 3 || size > 21)
    {
        cout << endl;
        cout << "Number is either too big or small. Please rechoose." << endl;
        cout << endl;
        cout << "Side Size: ";
        cin >> size;
    }
    
    while (choice != 'n')
    {
        //the beginning for loop will create one single + in the beginning of the line.
        for (int FirstL = 0; FirstL < 1; FirstL++)
        {
            cout << '+';
                
            //for loop creates the line across the top. It starts at two because we have + on both ends of the line.
            for (int straightTop = 2; straightTop < size; straightTop++)
            {
                cout << "-";
            }
                
            cout << "+"; //creates the + at the end of the line. (For top row)
            cout << endl;
                
            //this for loop creates the vertical line. It is a nedsted for loop because the for loop has to create two of the same exact vertical lines side by side but with space inbetween them so the line goes across to the other side.
            for (int VerticalLine = 2; VerticalLine < size; VerticalLine++)
            {
                cout << "|";
                        
                for (int verticalSpace = 2; verticalSpace < size; verticalSpace++)
                {
                    cout << " "; //creates the space between both vertical lines.
                }
                        
                    cout << "|\n"; //\n is another form of endl;
                }
                
                cout << "+"; //creates + at the end of the vertical line
                
                //for loop creates the bottom line.
                for (int straightBottom = 2; straightBottom < size; straightBottom++)
                {
                    cout << "-";
                }   
                
                //creates + at the end of the line.
                cout << "+" << endl;
        }
        
        cout << "Great! Would you like to play again?: ";
        cin >> choice;
        
        if (choice == 'n')
        {
            cout << "Thanks for playing!" << endl;
        }
        
        if (choice == 'y')
        {
            size = 0;
        }
    }
    
    return 0;
}
#包括
使用名称空间std;
int main()
{
//声明变量
整数大小;
字符选择;

您可以询问运行循环之外的大小。因此,您的程序流如下所示

  • 要尺码
  • 打印矩形
  • 询问他们是否需要另一个
  • 如果是,则返回步骤2
  • 您需要将该大小代码移动到循环中。因此,它将如下所示:

    while (choice != 'n')
    {
        
        cout << "Side size = ";
        cin >> size;
        
        while (size < 3 || size > 21)
        {
            cout << endl;
            cout << "Number is either too big or small. Please rechoose." << endl;
            cout << endl;
            cout << "Side Size: ";
            cin >> size;
        }
    
        // ...
    

    实例:

    您要求运行循环之外的大小。因此,您的程序流如下所示

  • 要尺码
  • 打印矩形
  • 询问他们是否需要另一个
  • 如果是,则返回步骤2
  • 您需要将该大小代码移动到循环中。因此,它将如下所示:

    while (choice != 'n')
    {
        
        cout << "Side size = ";
        cin >> size;
        
        while (size < 3 || size > 21)
        {
            cout << endl;
            cout << "Number is either too big or small. Please rechoose." << endl;
            cout << endl;
            cout << "Side Size: ";
            cin >> size;
        }
    
        // ...
    

    实例:

    您的程序在
    while(choice!='n')
    的第一次迭代中初始化之前读取了
    choice
    。您的程序在
    while(choice!='n')的第一次迭代中初始化之前读取了
    choice
    。在过去的3天里,我一直在绞尽脑汁想办法让它发挥作用。你在5分钟内就解决了。谢谢你!我真是太感谢你了@scohe001@EduardoMunozAlvarez程序员最好的朋友是调试器。有了调试器,你可以一行一行地浏览程序,并观察它发生的过程。通常只要看看发生了什么就足够了。“哦,哦,蓝精灵。”再加上你遗漏的一点,把线移到需要的地方。过去3天我一直在努力想办法让它工作。你在5分钟内就解决了。谢谢你!我真是太感谢你了@scohe001@EduardoMunozAlvarez程序员最好的朋友是调试器他一行一行地编程,看着发生了什么。通常只要看着发生了什么就足够了。“哦,哦,蓝精灵。”然后加上你遗漏的一点,把一行移到需要的地方。