cin缓冲区有一些恼人的问题

问题描述 投票:1回答:1

这是我的一些代码。基本上,在我的主要我将用户传递给这个选择器菜单。当用户进行选择时,我将返回main,然后将它们传递给相应的类以执行其他功能。

例如,当用户选择“发送”时,它们将被传递给main,然后传递给一个函数,该函数收集有关发送位置的输入。然后回到main,然后回到一个函数,询问他们多少。这在第一次工作正常。

问题是,如果他们尝试发送另一个交易,它会自动使用之前输入的金额填写地址。用户必须从控制台行自行删除它。基本上,数量被卡在cin缓冲区中并自动填充变量。我尝试使用getline,它也有同样的问题。我可以使用一个小函数来清除使用cin.clear()和cin.ignore(1000,'\ n')的cin缓冲区,它解决了我的问题,但是用户必须在输入后输入额外的时间。

我把问题隔离到菜单上。当我从程序中排除菜单时,问题就消失了。菜单是一项正在进行中的工作,我知道它不漂亮或不完美。我无法弄清楚为什么它会这样做。请帮忙,我要撕掉我的头发。

此外,问题不在代码的if(刷新){...}部分,我尝试排除,问题仍然存在。

菜单头文件只有一些私有变量和向量声明。我很乐意根据要求发布额外的代码。

menu.cpp

#include "menu.h"
#include "KMD_COMMANDS.h"
#include <windows.h>

std::string menu::userInterface()
{
    KMD_COMMANDS displayInfo;

    bool selecting = true;  //
    refresh = true;         //
    numRight = 3;           //reset variables back to default
    options = oDWB;         // < string vector

    system("cls");

    hideCursor();
    while(selecting)
    {
        numLeft = 3 - numRight;     //sets the number of left movements available

        if(refresh)   //only refresh the screen when user makes an input. I plan to use a console vector updating method in the future instead of "cls"... I know "cls" sucks
        {
            system("cls");

            std::cout << "Balance: ";
            displayInfo.getBalance();
            std::cout<<std::endl;
            std::cout << "Public Address: ";
            displayInfo.getPubKey();
            std::cout<<std::endl;

            for(int i = 0; i < options.size(); i++)
            {
                std::cout << options[i];
            }

            refresh = false;    //refresh is done
        }

        Sleep(100);     //this makes a delay so inputs are less sensitive

        if(GetAsyncKeyState(VK_RIGHT))
        {
            refresh = true;     //reset refresh variable so console updates

            switch(numRight)    //moves the selector around
            {
                case 1:
                    numRight--;
                    options = optionsDefault;   //sets the options selector
                    options[12] = "[";          //back to default state
                    options[14] = "]";          //and moves brackets
                    break;
                case 2:
                    numRight--;
                    options = optionsDefault;
                    options[8] = "[";
                    options[10] = "]";
                    break;
                case 3:
                    numRight--;
                    options = optionsDefault;
                    options[4] = "[";
                    options[6] = "]";
                    break;
                default:
                    break;
            }
        }

        if(GetAsyncKeyState(VK_LEFT))    //moves the selector around
        {
            refresh = true;

            switch(numLeft)
            {
                case 1:
                    numRight++;
                    options = optionsDefault;
                    options[0] = "[";
                    options[2] = "]";
                    break;
                case 2:
                    numRight++;
                    options = optionsDefault;
                    options[4] = "[";
                    options[6] = "]";
                    break;
                case 3:
                    numRight++;
                    options = optionsDefault;
                    options[8] = "[";
                    options[10] = "]";
                    break;
                default:
                    break;
            }
        }

        if(GetAsyncKeyState(VK_UP))     //takes users selection (changed to up for debugging purposes)
        {
            switch(numRight)    //takes selection choice based from number of right inputs remaining
            {
                case 1:
                    userChoice = "send";
                    return userChoice;
                    break;
                case 2:
                    userChoice = "unlock";
                    return userChoice;
                    break;
                case 3:
                    userChoice = "lock";
                    return userChoice;
                    break;
                default:
                    userChoice = "quit";
                    return userChoice;
                    break;
            }
        }
    }
}

这是传递用户收集信息的地方,这些信息位于:

#include "confirmSend.h"

#include <iostream>
#include <windows.h>

std::string confirmSend::sendToAddress()
{
    Sleep(100);     //delay so user doesn't accidentally input twice

    bool confirm = false;
    std::string addressInput;
    while(!confirm)
    {
        //std::cin.clear();
       // std::cin.ignore(1000,'\n');

        system("cls");
        std::cout << "Type cancel to cancel..." << std::endl;
        std::cout<<std::endl;
        std::cout << "Enter the address of where to send: ";
        std::cin >> addressInput;
        Sleep(800);
       // std::cout<<std::endl;

        confirm = true;
    }

    return addressInput;
}

int confirmSend::sendAmount()
{
    Sleep(100);     //delay so user doesn't accidentally input twice

    bool confirm = false;
    int amount;

    while(!confirm)
    {
       // std::cin.clear();
       // std::cin.ignore(1000,'\n');

        system("cls");
        std::cout << "type 0 to cancel..." << std::endl;
        std::cout<<std::endl;
        std::cout << "Enter how much to send:" << std::endl;
        std::cin >> amount;
        std::cout << std::endl;

        confirm = true;
    }

    return amount;
}
c++ winapi cin
1个回答
1
投票

输入每个std :: cin然后单击“enter”后,cin缓冲区中会有一个'\ n',你需要一些方法来消除它。如果只有'\ n'或其他,请使用getchar()作为一个简单的解决方案。如果'\ n'之前有更多字符,你可以使用getline()消除所有字符,直到它得到'\ n'。

© www.soinside.com 2019 - 2024. All rights reserved.