Search code examples
c++inputuser-inputcin

Limiting the number of characters of user input


I'm trying to limit # of characters a user can input.
It's not like when user inputs abcde, and I limit the input length to be 3,
and only abc is taken into account.

Is there a way to physically limit user from inputting more than certain amount of characters?
For example, if user trys to type 12345, and if I limit it to 3 characters, only 123 gets typed.

I've tried the following code:

cin.width(5);
cin >> n;

But I've realized it doesn't physically limit the user input, but only limits the buffersize of input.

Is there a way to do something like this?

+) I'm working on Console Application


Solution

  • You can't do it in a standard console window but if you use c++ to make your own window with your own input box then you have some more flexibility.