reading character from stdin

Discussion in 'C++' started by Mike, Mar 21, 2006.

  1. Mike

    Mike Guest

    I seem to be having a problem with the following piece of (simple) code:

    #include <iostream>

    using namespace std;

    int main()
    char ch;
    ch = cin.get();
    cout << "Got character...'" << ch << endl;

    This is using Visual C++ 6.0 (yes, I know. I'm a unix guy and don't like
    it very much). As soon as a character is read, I want to immediately
    display it. But, it wants to read until the newline before echoing the
    character. What am I doing wrong? Probably something stupid... :)

    The documentation says that it will read a single character, but it's
    reading an entire line!

    Mike, Mar 21, 2006
    1. Advertisements

  2. Mike

    Phlip Guest

    Complaints about MS and VC++ (and the Unices) are much more effective and
    useful when they are accurate, not just wild broadsides. Both suck.
    The method did indeed return one character. But the other end of the stream
    system is defined as buffering a string until you strike a turnaround
    character. Either change the turnaround character (at the level of the
    STDIN file handle), or use the non-Standard functions getch() or getche()
    to get a character. I can't remember if they are in <stdio.h> or <conio.h>.

    Type their name, and hit <F1> on that crummy peesashit Microsoft editor, and
    it will instantly tell you.
    Phlip, Mar 21, 2006
    1. Advertisements

  3. Mike

    Mike Guest

    Sure, but it has been my experience that most look negatively on M$
    products. Depends who you ask I guess. Are you a Mac advocate?
    Heh... thanks. I'll give that a shot. I think <conio.h> has the
    definition for getch(), but is there no way to accomplish this using
    istream or any if its derivations?
    Mike, Mar 21, 2006
  4. Mike

    TB Guest

    Mike skrev:
    Perhaps, but it's rather amazing that some people, even first-posters,
    somehow assume that they must write anti-MS statements as soon as they
    mention using any of their products.
    TB, Mar 22, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.