Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading single character from standard input stream

Status
Not open for further replies.

smithpd

Programmer
Jul 8, 2002
20
0
0
US
I want to read a single character from the input stream and take immediate action when I do so. For example,

char answer;
cout << &quot;Type 'y' to continue&quot; << endl;
answer = cin.get();
if (answer != 'y') return 0;
// continue

The problem is that when I use cin.get(), getchar(), cin.peek(), cin.read(), etc., it seems to be necessary to terminate the input of &quot;y&quot; with a newlin character, (the Enter key, or &quot;\n&quot;) before the code will begin reading the buffer and start processing the input stream. How can I avoid this, i.e., to have the program examine the answer character immediately and take action without entering a newlin?
 
That's just how cin works...there is no data until you press ENTER. You'll have to find another keyboard reading library if you want to capture individual keystrokes and process them in real time.

Ian
 
Thanks, Ian. I was afraid of that from my test results. Does anyone know of another keyboard input library, preferably contained in standard C / C++? If not that, then something free. Maybe with source? I would like to run it on different platforms.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top