Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Getting Input string without Pressing Enter of particular length

Reply
Thread Tools

Getting Input string without Pressing Enter of particular length

 
 
bhutho
Guest
Posts: n/a
 
      10-26-2011
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!
 
Reply With Quote
 
 
 
 
Richard Damon
Guest
Posts: n/a
 
      10-26-2011
On 10/26/11 6:04 PM, bhutho wrote:
> 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.


 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      10-27-2011
bhutho <(E-Mail Removed)> wrote:
> 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).
 
Reply With Quote
 
Turma 15 FCM
Guest
Posts: n/a
 
      10-28-2011
On Oct 26, 11:04*pm, bhutho <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Single character input without pressing 'Enter' unni.tallman Ruby 3 09-20-2006 12:56 PM
Single character input without pressing 'Enter' unni.tallman Python 3 09-20-2006 11:37 AM
Input without pressing enter? LaBird C Programming 6 10-16-2003 10:26 AM
Input without pressing Enter paul C++ 1 08-04-2003 02:59 PM
input=text fires Submit when pressing Enter Jerry ASP .Net 4 07-25-2003 01:38 PM



Advertisments