Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit visible user input when using std::cin?

I'm looking for a method to limit the visible user input using std::cin.

#include <iostream>
int main()
{
   std::cout << "Enter your planet:\n";
   string planet;
   std::cin >> planet; // During the prompt, only "accept" x characters
 }

What the user sees if they enter earth or any other word exceeding 4 characters before pressing enter:

Enter your planet:
eart

This is assuming the character limit is 4, note that the 'h' is missing. The console does not display any other character once it has exceeded the character limit. and this is before you press the enter key.

Kinda like typing in an input box like password fields, but it only allows 5 characters, so typing any other character goes unnoticed

A better analogy would be the maxlength attribute for text input in HTML.

like image 997
Nicholas Theophilus Avatar asked Oct 18 '22 01:10

Nicholas Theophilus


1 Answers

That can't be achieved portably, because OS consoles aren't part of C++ standard. In windows, you could use <windows.h> header - it provides console handles etc., but since you didn't specify OS you are using, the is no point in posting windows-only code here (since it might not meet your needs).


EDIT:

Here is (not perfect) code that will limit visible input of the user:

#include <iostream>
#include <windows.h>
#include <conio.h>
int main()
{
    COORD last_pos;
    CONSOLE_SCREEN_BUFFER_INFO info;
    std::string input;
    int keystroke;
    int max_input = 10;
    int input_len = 0;
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

    std::cout << "Input (max 10) characters, press ENTER to prompt:" << std::endl;

    GetConsoleScreenBufferInfo(handle, &info);
    last_pos = info.dwCursorPosition;

    while(true)
    {
        if(kbhit())
        {
            keystroke = _getch();
            //declare what characters you allow in input (here: a-z, A-Z, 0-9, space)
            if(std::isalnum(keystroke) || keystroke == ' ') 
            {
                if(input_len + 1 > max_input)
                    continue;

                ++input_len;

                std::cout << char(keystroke);
                input += char(keystroke);

                GetConsoleScreenBufferInfo(handle, &info);
                last_pos = info.dwCursorPosition;
            }
            else if(keystroke == 8) //backspace
            {
                if(input_len - 1 >= 0)
                {
                    --input_len;
                    input.pop_back();

                    COORD back_pos {short(last_pos.X-1), last_pos.Y};

                    SetConsoleCursorPosition(handle, back_pos);
                    std::cout << ' ';
                    SetConsoleCursorPosition(handle, back_pos);

                    GetConsoleScreenBufferInfo(handle, &info);
                    last_pos = info.dwCursorPosition;
                }
            }
            else if(keystroke == 13) //enter
            {
                std::cout << std::endl;
                break;
            }
        }
    }

    std::cout << "You entered: " << std::endl
              << input << std::endl; 
}
like image 128
xinaiz Avatar answered Nov 14 '22 12:11

xinaiz