Picle Wicle
Picle Wicle

Reputation: 39

Having some annoying issues with the cin buffer

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.

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.

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.

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

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

Upvotes: 1

Views: 77

Answers (1)

Drake Wu
Drake Wu

Reputation: 7170

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'.

Upvotes: 1

Related Questions