Getting Input string without Pressing Enter of particular length

B

bhutho

Hello All,
I want to get the user input of string of particular length say three character after which user need not to press enter button and I should get the string in the variable.

string name;
cout << "Please enter the the three initial letters of your name: ";
cin >> name;


But the problem is I dont want the user to press enter after pressing the three initial. How can I do it?
_getch() allows me to get only one char. But I want to get the string of particular length.
Please help!
 
R

Richard Damon

Hello All,
I want to get the user input of string of particular length say three character after which user need not to press enter button and I should get the string in the variable.

string name;
cout<< "Please enter the the three initial letters of your name: ";
cin>> name;


But the problem is I dont want the user to press enter after pressing the three initial. How can I do it?
_getch() allows me to get only one char. But I want to get the string of particular length.
Please help!

The C++ Standard provides no such operation. If you have a function
provided that gets one character, then you can write it yourself with 3
calls to it. Just watch out that you make sure there is a sequence point
between calls so you know the order they are executed in.

Even better, generalize this by write a function to read n characters
and return the resultant string.

Yes, it will not be quite as "pretty" as cin>>name; but that is because
you are asking for something that streams were not designed for.
 
J

Juha Nieminen

bhutho said:
I want to get the user input of string of particular length say three character after which user need not to press enter button and I should get the string in the variable.

Unbuffered input is system-specific and thus not covered by the C++
standard. You'll have to use a system-specific way of doing this. Most
systems have such functions but naturally depends on your OS (and
compiler).
 
T

Turma 15 FCM

Hello All,
I want to get the user input of string of particular length say three character after which user need not to press enter button and I should get thestring in the variable.

string name;
cout << "Please enter the the three initial letters of your name: ";
cin >> name;

But the problem is I dont want the user to press enter after pressing thethree initial. How can I do it?
_getch() allows me to get only one char. But I want to get the string of particular length.
Please help!

A good library to achieve what you want is ncurses (pdcurses for
windows), also it's cross platform between windows and linux, apart
from an header include.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top