cin >> c

Discussion in 'C++' started by Kevin W., Aug 23, 2004.

  1. Kevin W.

    Kevin W. Guest

    I'm writing a simple Unix console app. I need to take an input of a
    single character from the user and discard the rest.

    If I have:
    char c;
    cin >> c;
    And the user types the letter 'a', then presses Enter, 'a' gets put into c
    as I want. BUT, Enter remains as the next character in the stream, so if
    I then do a getline(cin, word), I get an empty string in word. The other
    requirement I need is that if the user just presses Enter without entering
    a letter, c should be assigned to '\n'.

    Ideally, it should process the character (whatever it is) without the user
    having to press Enter.

    --
    Kevin W :)
    Opera/CSS/webdev blog: http://www.exclipy.com/
    Using Opera: http://www.opera.com/m2/
    Kevin W., Aug 23, 2004
    #1
    1. Advertising

  2. "Kevin W." wrote:
    >
    > I'm writing a simple Unix console app. I need to take an input of a
    > single character from the user and discard the rest.
    >
    > If I have:
    > char c;
    > cin >> c;
    > And the user types the letter 'a', then presses Enter, 'a' gets put into c
    > as I want. BUT, Enter remains as the next character in the stream, so if
    > I then do a getline(cin, word), I get an empty string in word. The other
    > requirement I need is that if the user just presses Enter without entering
    > a letter, c should be assigned to '\n'.


    The simplest thing to fulfill your requirement is to use getline
    for entering that single character too. Just use the first character
    of the read line and be done with it. If what you read is an empty
    line, then just assume 'c' was pressed.

    Doing the above is often one of the simplest solutions to most
    input problems:

    just read a complete line as a string and use from that line
    whatever you need. This also may mean to convert things on your
    own or to extract the things you need from the input line as
    required.

    >
    > Ideally, it should process the character (whatever it is) without the user
    > having to press Enter.


    Not possible with standard C++

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Aug 23, 2004
    #2
    1. Advertising

  3. OT - Re: cin >> c

    Kevin W. wrote:
    > Ideally, it should process the character (whatever it is) without the
    > user having to press Enter.


    This would require changing the attributes of the input device. Something
    like 'raw' input or disabling canonical mode input processing, which would be
    arranged using ioctl or stty or some such. I think you would first have to
    find out what device you are receiving your standard input from. It's a while
    since I used UNIX so I can't be very specific I'm afraid. You should do
    better asking in a UNIX newsgroup.

    --
    Regards,

    Joe Hotchkiss,
    http://joe.hotchkiss.com

    XXXXXXXXXXXXXXXXXXXXXXXXX
    X joe.hotchkiss X
    X at baesystems.com X
    XXXXXXXXXXXXXXXXXXXXXXXXX
    Joe Hotchkiss, Aug 23, 2004
    #3
  4. Kevin W.

    Mike Wahler Guest

    "Kevin W." <> wrote in message
    news:eek:...
    > I'm writing a simple Unix console app. I need to take an input of a
    > single character from the user and discard the rest.
    >
    > If I have:
    > char c;
    > cin >> c;
    > And the user types the letter 'a', then presses Enter, 'a' gets put into c
    > as I want. BUT, Enter remains as the next character in the stream, so if
    > I then do a getline(cin, word), I get an empty string in word. The other
    > requirement I need is that if the user just presses Enter without entering
    > a letter, c should be assigned to '\n'.


    char c(0);
    std::string word;
    std::getline(std::cin, word);
    c = (word + '\n')[0];

    > Ideally, it should process the character (whatever it is) without the user
    > having to press Enter.


    Not possible with standard C++.

    -Mike
    Mike Wahler, Aug 23, 2004
    #4
  5. Using Windows Specific libraries can be done like this:

    #include <iostream>
    #include <string>
    #include <conio.h>

    void main( void )
    {
    std::string strg = "";
    for(char ch = 0; ch != ' '; ch = _getch(),strg += ch);
    std::cout << strg << std::endl;
    }


    "Kevin W." <> wrote in message news:<>...
    > I'm writing a simple Unix console app. I need to take an input of a
    > single character from the user and discard the rest.
    >
    > If I have:
    > char c;
    > cin >> c;
    > And the user types the letter 'a', then presses Enter, 'a' gets put into c
    > as I want. BUT, Enter remains as the next character in the stream, so if
    > I then do a getline(cin, word), I get an empty string in word. The other
    > requirement I need is that if the user just presses Enter without entering
    > a letter, c should be assigned to '\n'.
    >
    > Ideally, it should process the character (whatever it is) without the user
    > having to press Enter.
    John Pazarzis, Aug 23, 2004
    #5
  6. Kevin W.

    Kevin W. Guest

    > The simplest thing to fulfill your requirement is to use getline
    > Just use the first character of the read line and be done with it.


    Thanks.

    --
    Kevin W :)
    Opera/CSS/webdev blog: http://www.exclipy.com/
    Using Opera: http://www.opera.com/m2/
    Kevin W., Aug 24, 2004
    #6
  7. Kevin W.

    K Campbell Guest

    Try doing this:
    char c;
    cin>>c;
    cin.ignore();
    cin.getline(name, int);

    or, if you want to just capture one character, without the user having
    to press enter:
    (you must #include <conio.h>)
    char c = getch();
    cin.getline(name, int);

    Hope i've been of some help.
    Kieran





    "Kevin W." <> wrote in message news:<>...
    > I'm writing a simple Unix console app. I need to take an input of a
    > single character from the user and discard the rest.
    >
    > If I have:
    > char c;
    > cin >> c;
    > And the user types the letter 'a', then presses Enter, 'a' gets put into c
    > as I want. BUT, Enter remains as the next character in the stream, so if
    > I then do a getline(cin, word), I get an empty string in word. The other
    > requirement I need is that if the user just presses Enter without entering
    > a letter, c should be assigned to '\n'.
    >
    > Ideally, it should process the character (whatever it is) without the user
    > having to press Enter.
    K Campbell, Aug 24, 2004
    #7
  8. Kevin W.

    Mike Wahler Guest

    "K Campbell" <> wrote in message
    news:...
    > Try doing this:
    > char c;
    > cin>>c;
    > cin.ignore();


    This will extract and discard the first of possibly
    several characters following the one that gets extracted
    and stored in 'c'. E.g. if I input "abc", character 'a'
    gets put into 'c', character 'b' is thrown away, and character
    'c' remains in the stream. You can give an argument of
    'std::numeric_limits<std::streamsize>::max()' to 'ignore()',
    and it will ignore all pending characters, not just the first.

    > cin.getline(name, int);
    >
    > or, if you want to just capture one character, without the user having
    > to press enter:
    > (you must #include <conio.h>)


    Not a standard C++ header. Please don't post such things here.

    > char c = getch();


    Not a standard C++ function. Please don't post such things here.


    > cin.getline(name, int);
    >
    > Hope i've been of some help.


    Hmm.

    -Mike
    Mike Wahler, Aug 24, 2004
    #8
    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. Gregory W. Ernest

    Java equivalent of cin

    Gregory W. Ernest, Jan 18, 2005, in forum: Java
    Replies:
    3
    Views:
    24,742
    gstkein
    Aug 3, 2008
  2. TaiwanNoWhere

    cin and cin.clear() problem

    TaiwanNoWhere, Oct 13, 2003, in forum: C++
    Replies:
    8
    Views:
    4,090
    P.J. Plauger
    Oct 17, 2003
  3. Chris Mantoulidis

    std::cin.ignore() and std::cin.clear()

    Chris Mantoulidis, Jan 6, 2004, in forum: C++
    Replies:
    5
    Views:
    17,137
    Kevin Saff
    Jan 6, 2004
  4. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,662
    Alex Vinokur
    Mar 6, 2005
  5. Fernando
    Replies:
    4
    Views:
    1,644
    Fernando
    Nov 16, 2011
Loading...

Share This Page