Getting Input string without Pressing Enter of particular length

Discussion in 'C++' started by bhutho, Oct 26, 2011.

  1. bhutho

    bhutho Guest

    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!
     
    bhutho, Oct 26, 2011
    #1
    1. Advertising

  2. 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.
     
    Richard Damon, Oct 26, 2011
    #2
    1. Advertising

  3. bhutho <> 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).
     
    Juha Nieminen, Oct 27, 2011
    #3
  4. bhutho

    Turma 15 FCM Guest

    On Oct 26, 11: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 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.
     
    Turma 15 FCM, Oct 28, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry
    Replies:
    4
    Views:
    1,191
    John Saunders
    Jul 25, 2003
  2. paul
    Replies:
    1
    Views:
    531
    Jonathan Mcdougall
    Aug 4, 2003
  3. LaBird

    Input without pressing enter?

    LaBird, Oct 16, 2003, in forum: C Programming
    Replies:
    6
    Views:
    1,213
    Joona I Palaste
    Oct 16, 2003
  4. unni.tallman
    Replies:
    3
    Views:
    1,647
    Fredrik Lundh
    Sep 20, 2006
  5. unni.tallman
    Replies:
    3
    Views:
    209
    James Edward Gray II
    Sep 20, 2006
Loading...

Share This Page