cin缓冲区有一些烦人的问题 [英] Having some annoying issues with the cin buffer

查看:80
本文介绍了cin缓冲区有一些烦人的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的一些代码。基本上,我主要是将用户转到该选择器菜单。当用户做出选择时,我将返回主菜单,然后将它们传递给适当的类以执行进一步的功能。

Here is my bit of code. Basically, in my main i'm passing the user to this selector menu. When the user makes a selection, i'm passing back to main and then passing them to the appropriate class to perform further functions.

例如,当用户选择发送,它们被传递给main,然后传递给一个函数,该函数收集有关发送位置的输入。然后返回main,然后返回一个函数,询问他们多少钱。

For example, when the user selects "send", they get passed to main and then passed to a function which collects input about where to send. Then back to main, then back to a function that asks them how much. This works fine the first time around.

问题是,如果他们尝试发送另一笔交易,它将自动用先前输入的金额填充地址。用户必须自己从控制台行将其删除。基本上,该数量卡在cin缓冲区中并自动填充变量。我尝试使用getline,它也有同样的问题。我可以使用一个小函数通过cin.clear()和cin.ignore(1000,'usen')来清除cin缓冲区,它可以解决我的问题,但是用户必须在输入后再输入一次。

The problem is, if they try to send another transaction, it automatically fills the address with the amount entered previously. The user has to delete it themselves from the console line. Basically, the amount is getting stuck in the cin buffer and automatically filling the variable. I'v tried using getline, and it has the same problem. I can use a little function to clear the cin buffer using cin.clear() and cin.ignore(1000,'\n') and it fixes my problem, but then the user has to hit enter an extra time after inputting.

我将问题隔离到菜单中。当我从程序中排除菜单时,问题消失了。菜单尚在进行中,我知道它不美观或不够精美。我不知道为什么要这么做。

I'v isolated the problem to the menu. When I exclude the menu from my program, the problem disappears. The menu is a work in progress, I know it's not pretty or polished. I can't figure out why it's doing it though. Please help, i'm about to rip my hair out.

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

Also, the problem isn't in the if(refresh){...} part of the code, i'v tried excluding that and the problem persists.

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

the menu header file just has some private variables and vector declarations. I'll be happy to post extra code upon request.

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

此处是传递用户收集信息和cin所在位置的地方:

Here is where the user is passed to collect info, where the cins are located:

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


推荐答案

在每个std :: cin之后已输入,然后单击输入,cin缓冲区中将剩下一个 \n,您需要某种方法来消除它。如果只有一个 n或其他字母,请使用 getchar()作为简单的解决方案。并且如果在'\n'之前还有更多字符,则可以使用 getline()消除所有字符,直到得到'\n'。

After every std::cin has been inputted and then click "enter", there will be a '\n' left in the cin buffer, and you need some method to eliminate it. If there is only a '\n' or others, use getchar() as a simple solution. And if there are more characters before '\n', You can use getline() to eliminate all of them until it get '\n'.

这篇关于cin缓冲区有一些烦人的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆