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

I am looking for a way to limit the user's visible input with 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 

The character limit is assumed to be 4, note that 'h' missing. The console does not display any other characters if it has exceeded the character limit. and this is before pressing the enter key.

It looks like entering text into an input field, such as a password field, but it only accepts 5 characters, so entering any other character goes unnoticed.

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

+5
source share
2 answers

This cannot be done portable, since OS consoles are not part of the C ++ standard. In windows, you can use the <windows.h> header - it provides console descriptors, etc., but since you did not specify the OS you are using, it makes no sense to publish only Windows code here (as this may not suit your needs) .


EDIT:

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

 #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: az, AZ, 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; } 
+3
source

After several days of experimentation, I found another solution that seems to be fairly easy to understand, since it is a little newbie and does not require any knowledge of window programming.

Note: The library function conio.h _getch() can be easily replaced with the getchar() function;

I'm not saying that the previous answer was out of order, but this solution seems like newbies with basic c++ knowledge

 char ch; string temp; ch = _getch(); while(ch != 13)// Character representing enter { if(ch == '\b'){ //check for backspace character if(temp.size() > 0) // check if string can still be reduced with pop_back() to avoid errors { cout << "\b \b"; // temp.pop_back(); // remove last character } } else if((temp.size() > 0) || !isalpha(ch))// checks for limit, in this case limit is one { //character and also optional checks if it is an alphabet cout << '\a'; // for a really annoying sound that tells you know this is wrong }else { temp.push_back(ch); // pushing ch into temp cout << ch; // display entered character on screen } ch = _getch(); } 

Perhaps this may affect some settings, because it is definitely not ideal, but I think it is easy enough to understand, at least I hope that

0
source

All Articles