How to get "cin >> char" not to need "Enter"?

How to get "cin >> char" not to need "Enter"?

Post by Pani » Tue, 20 Apr 1999 04:00:00



I am wondering if there is someway I can get cin >> char;  to act like
getch().  I am writing a program in C++ and I need to not have to hit enter
after inputting a character.  Is ther any way to do this?  Please let me
know.
Thanks,
Bri
 
 
 

How to get "cin >> char" not to need "Enter"?

Post by Robert Wues » Tue, 20 Apr 1999 04:00:00



> I am wondering if there is someway I can get cin >> char;  to act like
> getch().  I am writing a program in C++ and I need to not have to hit enter
> after inputting a character.  Is ther any way to do this?  Please let me
> know.
> Thanks,
> Bri

Do this first (turn off canonical mode):

#include <termios.h>

        struct termios tio;
        tcgetattr(0,&tio);
        tio.c_lflag &= ~ICANON;
        tcsetattr(0,TCSANOW,&tio);

Not sure this works with streams, but it should.

Robert

 
 
 

How to get "cin >> char" not to need "Enter"?

Post by Karl Heye » Tue, 20 Apr 1999 04:00:00



> I am wondering if there is someway I can get cin >> char;  to act like
> getch().  I am writing a program in C++ and I need to not have to hit enter
> after inputting a character.  Is ther any way to do this?  Please let me
> know.

That's because your terminal is buffering (the default), use stty before and
after the program to change yout terminal settings or you can use ncurses
library.

karl

 
 
 

How to get "cin >> char" not to need "Enter"?

Post by Jim Fol » Wed, 21 Apr 1999 04:00:00


If you're going to use c++, as it looks like you are, you might be able
to get away with cin.peek()


>I am wondering if there is someway I can get cin >> char;  to act like
>getch().  I am writing a program in C++ and I need to not have to hit enter
>after inputting a character.  Is ther any way to do this?  Please let me
>know.
>Thanks,
>Bri

--

ACORN techie <http://www.acorn.net>
      AOL/IM Jim Foltz
 
 
 

How to get "cin >> char" not to need "Enter"?

Post by Pani » Fri, 23 Apr 1999 04:00:00


Could you explain line by line your code below?
Quote:> Do this first (turn off canonical mode):

> #include <termios.h>

> struct termios tio;
> tcgetattr(0,&tio);
> tio.c_lflag &= ~ICANON;
> tcsetattr(0,TCSANOW,&tio);

 
 
 

How to get "cin >> char" not to need "Enter"?

Post by Pani » Fri, 23 Apr 1999 04:00:00


Nope, cin.peek() still requires me to hit enter after asking for input.


> If you're going to use c++, as it looks like you are, you might be able
> to get away with cin.peek()


> >I am wondering if there is someway I can get cin >> char;  to act like
> >getch().  I am writing a program in C++ and I need to not have to hit
enter
> >after inputting a character.  Is ther any way to do this?  Please let me
> >know.
> >Thanks,
> >Bri

> --

> ACORN techie <http://www.acorn.net>
>       AOL/IM Jim Foltz

 
 
 

How to get "cin >> char" not to need "Enter"?

Post by Pani » Fri, 23 Apr 1999 04:00:00



Quote:> That's because your terminal is buffering (the default), use stty before
and
> after the program to change yout terminal settings or you can use ncurses
> library.

Could you explain what stty is and how I can use ncurses to use stty?
Thanks,
Bri
 
 
 

How to get "cin >> char" not to need "Enter"?

Post by Robert Wues » Fri, 23 Apr 1999 04:00:00



> Could you explain line by line your code below?

> > Do this first (turn off canonical mode):

> > #include <termios.h>

> > struct termios tio;
> > tcgetattr(0,&tio);
> > tio.c_lflag &= ~ICANON;
> > tcsetattr(0,TCSANOW,&tio);

man termios
 
 
 

How to get "cin >> char" not to need "Enter"?

Post by Paul J. Ride » Mon, 26 Apr 1999 04:00:00


Hello Panic,
    If all you are trying to do is to get a single character. Why don't you
just use:

char aCharacter;               //your character
cin.get( aCharacter );      //this just gets one character and places it in
'aCharacter'

You don't have to mess with termio to do this either.


> I am wondering if there is someway I can get cin >> char;  to act like
> getch().  I am writing a program in C++ and I need to not have to hit enter
> after inputting a character.  Is ther any way to do this?  Please let me
> know.
> Thanks,
> Bri

 
 
 

How to get "cin >> char" not to need "Enter"?

Post by Georges HANS » Mon, 26 Apr 1999 04:00:00


: I am wondering if there is someway I can get cin >> char;  to act like
: getch().  I am writing a program in C++ and I need to not have to hit enter
: after inputting a character.  Is ther any way to do this?  Please let me
: know.
: Thanks,
: Bri

you put the lines

#include <stdlib.h>

/* and in your program
in the begining */
system("stty -icanon");

/* and at the end */

system("stty icanon"); // to recover the usual state

Georges Hansel

 
 
 

How to get "cin >> char" not to need "Enter"?

Post by Pani » Mon, 26 Apr 1999 04:00:00


Well I don't know if you realize I am trying to get input from the keyboard.
And I have tried cin.get(ch) and it does require me hit enter after
inputting a char.



> Hello Panic,
>     If all you are trying to do is to get a single character. Why don't
you
> just use:

> char aCharacter;               file://your character
> cin.get( aCharacter );      file://this just gets one character and places
it in
> 'aCharacter'

> You don't have to mess with termio to do this either.


> > I am wondering if there is someway I can get cin >> char;  to act like
> > getch().  I am writing a program in C++ and I need to not have to hit
enter
> > after inputting a character.  Is ther any way to do this?  Please let me
> > know.
> > Thanks,
> > Bri

 
 
 

How to get "cin >> char" not to need "Enter"?

Post by Pani » Mon, 26 Apr 1999 04:00:00


It almost works now.  My code so far is included at the bottom.  What
happens is now I don't have to hit enter after a char, but it now requires
me to input 4 chars, and it then it continues.  For example if I run it:

Quote:>> input: <abcd>  // note without a char return.
>> output: a

I suppose this has something to do with the size of the input buffer?  Is
there a way to change the stty buffer?
Thanks so far,
Bri

#include <cstdlib>
#include <iostream>
using namespace std;
void main(){
        system("stty -icanon");
       char ch;
        cin.get(ch);
        cout << endl << ch << endl;
        system("stty icanon");

Quote:}

 
 
 

How to get "cin >> char" not to need "Enter"?

Post by Pani » Mon, 26 Apr 1999 04:00:00


It worked, yeah...



> > Could you explain line by line your code below?

> > > Do this first (turn off canonical mode):

> > > #include <termios.h>

> > > struct termios tio;
> > > tcgetattr(0,&tio);
> > > tio.c_lflag &= ~ICANON;
> > > tcsetattr(0,TCSANOW,&tio);

> man termios

 
 
 

How to get "cin >> char" not to need "Enter"?

Post by Byron A Je » Mon, 26 Apr 1999 04:00:00



-Nope, cin.peek() still requires me to hit enter after asking for input.

That's correct. These processes happen at two different levels. The line
buffering is done by the OS. That means when the application calls for a
read, the OS buffers the input until the newline is pressed. The application
is blocked until then.

So the termios ioctls tell the OS to go into unbuffed mode at which point
it'll return from the read as soon as any character is input.

-

-> If you're going to use c++, as it looks like you are, you might be able
-> to get away with cin.peek()
->
[deletia]