reading character from stdin

M

Mike

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!

Thanks.
 
P

Phlip

Mike said:
ch = cin.get();
This is using Visual C++ 6.0 (yes, I know. I'm a unix guy and don't like
it very much).

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 documentation says that it will read a single character, but it's
reading an entire line!

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.
 
M

Mike

Phlip said:
Complaints about MS and VC++ (and the Unices) are much more effective and
useful when they are accurate, not just wild broadsides. Both suck.

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?
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.

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?
 
T

TB

Mike skrev:
Sure, but it has been my experience that most look negatively on M$
products.

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.
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top