0

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

4
  • You could read each key and then do the processing when the user types \r or at 3. But that wouldn't block the extra characters (it would simply accept 3 characters as the users input and move on without waiting for a \r). Commented Jun 19, 2013 at 1:38
  • Hmm, I wish I could do something like this. Commented Jun 19, 2013 at 1:48
  • 4
    What do you mean by physically? You want a hand to come out of the screen and slap the user when she attempts to type 4-th character ? :) Commented Jun 19, 2013 at 3:11
  • @tony I had no idea how else to describe it :/ Commented Jun 20, 2013 at 3:00

3 Answers 3

2

You could do some weird thing like reading individual characters one at a time, and if they don't hit return by the 4th character, say invalid input and then make them start over, but it's cleaner and easier to just call long input invalid after they try to submit it.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

5 Comments

Is it possible to do something like this?
@Haxify Ofcourse! many games are programmed in c++ and have similar restrictions for some parts (such as username selection). However you cant really do this with out of the box c++. You need to find yourself a library. Oh and I meant to say you cant do it in a console. Sorry, typo.
I could make it work in a console. It wouldn't be as pretty as it could be, but it wouldn't be hard.
@chris Then you should post the outline of the answer.
@Dgrin91, I don't know... It might do more harm than good and it'd be Windows-only.
0

You may be searching for this:--

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string MyInput;
    std::cout << "INPUT HERE: ";
    std::getline(cin,MyInput);
    if (MyInput.length() == 3)
    {
        std::cout << "OKAY IT IS THREE CHARS" << std::endl;
    }
    else if (!(MyInput.length() == 3))
    {
        MyInput.erase(MyInput.begin()+3,MyInput.end());
        std::cout << "Look: " << MyInput << std::endl;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.