Jump to content

OT(Sorry) C++ Question!


Guest Rollstoy

Recommended Posts

Guest Rollstoy

Sorry to disturb this forum, but I did not get an answer on the general forum, so I try here!

I want to code a program that reacts directly to keystrokes, without puffering! I am reading and reading, but nowhere do I find the right method to switch puffering off!

To clarify: I want the program to react to keystrokes immediately and not to puffer input until the "Enter" key is pressed!

How do I code this!?! I am so frustrated that I cannot figure out something as simple as this!!!!!

Regards and thank you in advance,

Thomm

Link to comment
Share on other sites

I've done this before, but off-hand I don't remember if it required any special handling. It may require some Assembler commands. I'll check my old programs when I get home tonight and let you know.

BeWary

------------------

"Liberty or Death?" Make it "Victory or Pretty Damned Badly Wounded", and I'm yours. - a prospective recruit during the American Revolution.

Link to comment
Share on other sites

Ok, I just found the function I think you need to use. It's called getch() or getchar(). It will return the first key in the input stream, so if you put it in a loop, you should be able to achieve your goal of not requiring an <enter> key press. However, I don't know if non-buffered is the correct word to use. I think the buffer is used regardless, as it is a part of the BIOS sub-system. Getch() simply returns a character from that buffer.

My copy of Borland is warning not to use this function in Win32 GUI apps, but there's probably something similar available for Win32.

Good luck,

BeWary

------------------

"Liberty or Death?" Make it "Victory or Pretty Damned Badly Wounded", and I'm yours. - a prospective recruit during the American Revolution.

Link to comment
Share on other sites

Guest Rollstoy

First of all, thank you, BeWary!!!! I am really glad that somebody found time to answer me!

So I tried the following under Linux/X-Windows.

#include<cstdio> // getchar()

#include<iostream>

...

cin.sync_with_stdio();

...

while ((i=getchar())!=EOF) cout << i;

But STILL I have to press Enter to flush the input buffer! Is this related to X-Windows? Like the way you mentioned for Windows Apps?

I searched numerous book in the universtity library but could not find any solution!

Still waiting for help ...

Thomm

[This message has been edited by Rollstoy (edited 03-01-2001).]

Link to comment
Share on other sites

Guest Rollstoy

Some traces lead to ...

cin.setbuf(0,0); // or

cin.pubsetbuf(0,0);

to disable buffering (set it to zero length).

But neither will compile ...

Link to comment
Share on other sites

I suggest you start out simple and then start calling other methods. For example, are you sure you need to call sync_with_stdio()?

Why are you using EOF in your while loop? I've not seen that associated with keyboard input.

With the disabling buffering methods, what is the error you're getting when compiling? Perhaps you haven't #included the right files? I've had that happen to me sometimes too. I think some methods are only made to be run on certain platforms.

I'll look tonight at my old programs and see how I got it to work. I didn't have a chance last night.

BeWary

------------------

"Liberty or Death?" Make it "Victory or Pretty Damned Badly Wounded", and I'm yours. - a prospective recruit during the American Revolution.

Link to comment
Share on other sites

Rolls ... try the "endl" stream operator. Itflushes the output buffer (or something like that). If you use getch() you have to put a endl into the stream for your output to show up.

while ((i=getchar())!=EOF) cout << i << endl;

MK

[This message has been edited by Kraut (edited 03-01-2001).]

Link to comment
Share on other sites

Ok, I just found a program I wrote that immediately responds to keyboard input. Here's a sample of the code:

#include <conio.h>

char ch;

ch=getch();

while(ch!='q')

{

cout <<ch; ... or whatever you want to do

.

.

.

ch=getch();

}

Don't know if that'll work in XWindows or Linux, but it works in DOS/Win.

BeWary

------------------

"Liberty or Death?" Make it "Victory or Pretty Damned Badly Wounded", and I'm yours. - a prospective recruit during the American Revolution.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...