473,789 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to determine end of std::cin

Good day,

I'm experimenting with unbuffered input at the moment. To get input I
basically use cin.get(). My problem are control sequences preceeded by an
ESC character (i.e. up, down, f-keys et cetera). They basically look like
this: <ESC><EXCODE><C ODE>, where EXCODE is a number describing the type of
key pressed and CODE is the actual key. I want to be able to decide whether
ESC or such a control key was pressed. Now if I do it like this:

char code = 0, excode = 0;
code = cin.get()
if (code == 27) {
excode = cin.get();
code = cin.get();
}

, then I will have proper codes for all control keys. If ESC is pressed,
though, the user will actually have to press three times - because
cin.get() seems to wait for actual input to appear in stdin.
Now I tried stuff like

char code = 0, excode = 0;
code = cin.get()
if (code == 27 && !cin.eof()) {
excode = cin.get();
code = cin.get();
}

, but that won't work, even if there is only the ESC character in stdin.

Is there another way to determine, whether the last character was read
from stdin? Am I doing anything wrong?

Thanks alot.
Mar 2 '07 #1
8 3470
On 2 Mar, 11:26, Johannes Meng <j...@jmeng.dew rote:

First, I'm no expert on standard keryboard input in C++.

However AFAIK std::cin is quite primitive and only accepts ASCII
characters. The characters you want are extended ASCII AFAIK and a
quick test on win32 seems to suggest that standard input simply
ignores them.

Unfortunately the proper way forward I think, is to start
investigating a GUI toolkit. These firstly use a different paradigm
from the "input as file" paradigm of standard C++. This is the event
based program paradigm, in which your application sits idling and the
system fires events at it to which it responds. Typical events are
mouse move and keyboard events , and you then write functions to
describe what your application should do in response.

The simplest way to try some event based programming I would guess is
to use another language such as Java. C++ has ( nor probably will ever
have) much interest in standardisng a GUI.

regards
Andy Little

Mar 2 '07 #2
kwikius wrote:
On 2 Mar, 11:26, Johannes Meng <j...@jmeng.dew rote:

First, I'm no expert on standard keryboard input in C++.

However AFAIK std::cin is quite primitive and only accepts ASCII
characters. The characters you want are extended ASCII AFAIK and a
quick test on win32 seems to suggest that standard input simply
ignores them.
Well, I don't know about win32, but here on linux, when, say, "arrow up" is
pressed, cin actually contains 27 91 65. Try the following:

#include <iostream>
using namespace std;

int main(int argc, char **argv)
{
string test;
cin >test;

for (int i = 0; i < test.length(); i++) {
cout << (int)test[i] << " ";
}

cout << endl;

return 0;
}

If you start the programm, then press "arrow up" and then "enter", it should
output "27 91 65".

That means that the codes are there - I just need a way to determine whether
there's still something left in cin after reading a 27 (code for esc).

--
Johannes Meng
Mar 2 '07 #3
kwikius wrote:
The simplest way to try some event based programming I would guess is
to use another language such as Java. C++ has ( nor probably will ever
have) much interest in standardisng a GUI.
I just noticed I might not have been clear about what I want: I do _not_
want to program a gui. It's all about the command line ;)

--
Johannes Meng
Mar 2 '07 #4
* Johannes Meng:
>kwikius wrote:
The simplest way to try some event based programming I would guess is
to use another language such as Java. C++ has ( nor probably will ever
have) much interest in standardisng a GUI.

I just noticed I might not have been clear about what I want: I do _not_
want to program a gui. It's all about the command line ;)
*Dragging up memories from the eighties*

You probably need to do input at a lower, system-specific level because
you need a timeout.

You should check whether there's some library that translates escape
sequences to something more digestible, not sure if 'curses' does that.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 2 '07 #5
On 2 Mar, 16:29, Johannes Meng <j...@jmeng.dew rote:
<...>
#include <iostream>
using namespace std;

int main(int argc, char **argv)
{
string test;
cin >test;

for (int i = 0; i < test.length(); i++) {
cout << (int)test[i] << " ";
}

cout << endl;

return 0;

}

If you start the programm, then press "arrow up" and then "enter", it should
output "27 91 65".
Aha . Works when I compile using gcc (in Windows) but in VC7.1 hangs
when I press arrow up) until I press an ascii character.

Not sure whether there is a right and wrong or if its implementaion
defined. I suspect implementation defined though.

The one common characteristic is that you have to press return to
flush the buffer and signal end of input AFAIK, before you can make
anything happen.

And to avoid that ( I think) you need to be able to monitor system
events ( e.g in a loop looking for e.g any new key pressed etc) which
isnt provided for in a standardised form in C++

Again though I'm no expert on this...

regards
Andy Little.


Mar 2 '07 #6
kwikius wrote:
Aha . Works when I compile using gcc (in Windows) but in VC7.1 hangs
when I press arrow up) until I press an ascii character.

Not sure whether there is a right and wrong or if its implementaion
defined. I suspect implementation defined though.

The one common characteristic is that you have to press return to
flush the buffer and signal end of input AFAIK, before you can make
anything happen.

And to avoid that ( I think) you need to be able to monitor system
events ( e.g in a loop looking for e.g any new key pressed etc) which
isnt provided for in a standardised form in C++

Again though I'm no expert on this...
Thanks for testing! Well, I use unbuffered input, thus there's no need to
press enter. That works using some stuff from termios.h, which is most
probably linux/unix(?) specific, though.

--
Johannes Meng
Mar 2 '07 #7
Alf P. Steinbach wrote:
*Dragging up memories from the eighties*
Hehe. That's about where I'm going ;P
>
You probably need to do input at a lower, system-specific level because
you need a timeout.

You should check whether there's some library that translates escape
sequences to something more digestible, not sure if 'curses' does that.
The thing is this: I'm currently coding an input prompt with bash-like
editing capabilities, history and so on. It's almost functional and really,
really small and I'd rather it didn't have any external dependencies. I'll
try looking for a suitable library anyways.

Thank you!

--
Johannes Meng
Mar 2 '07 #8
On 2 Mar, 17:05, Johannes Meng <j...@jmeng.dew rote:
kwikius wrote:
Aha . Works when I compile using gcc (in Windows) but in VC7.1 hangs
when I press arrow up) until I press an ascii character.
Not sure whether there is a right and wrong or if its implementaion
defined. I suspect implementation defined though.
The one common characteristic is that you have to press return to
flush the buffer and signal end of input AFAIK, before you can make
anything happen.
And to avoid that ( I think) you need to be able to monitor system
events ( e.g in a loop looking for e.g any new key pressed etc) which
isnt provided for in a standardised form in C++
Again though I'm no expert on this...

Thanks for testing! Well, I use unbuffered input, thus there's no need to
press enter. That works using some stuff from termios.h, which is most
probably linux/unix(?) specific, though.
Yes look like a Unix header to me:

http://www.opengroup.org/onlinepubs/...termios.h.html

regards
Andy Little
Mar 2 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2006
by: William Payne | last post by:
Hello, when I was writing a user-driven test program for a data structure I wrote, I encountered an annoying problem. The test program is laid out as a menu with several numbered options. The user selects one option by typing in its corresponding number (int). Depending on the choice he made, he may be asked to provide another integer (or no input at all). So all the user does is entering integers. I don't want the user to be able to cause...
5
12243
by: Chris Mantoulidis | last post by:
Let's say I have this: std::string s1; std::cin >> s1; This will read s1 from cin until it finds a space (or a newline, whichever comes first). Okay this works. But when I want to continue reading it reads what's left over in the cin, and well that's logical.
3
1923
by: yw | last post by:
Hi, When I use std::cin and std::cout to input some data, how can I ingore the cin by using the Enter key? For example: string sname; cout<<"Please input your name:"<<endl; cin>>sname; If I don't wan't to input anything, just press enter key to ingnore the cin
3
7096
by: puzzlecracker | last post by:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); I have seen it in faq - what does it do, exactly?
3
5324
by: moleskyca1 | last post by:
In C++ FAQ (15.5), I see this code: int i = 0; while (std::cin >x) { // RIGHT! (reliable) ++i; // Work with x ... } but I don't know how can while loop end? ostream &operator >>(...) return ostream &. how can that ever end the while loop? Is it because there is hidden operator to cast to int? I cannot figure out, please help?
8
15723
by: junw2000 | last post by:
Below is simple code: #include <iostream> int main(){ int i; while(1){ while(!(std::cin >i)){ std::cout<<"Invalid input i: "<<i<<'\n';
3
14689
by: Ralf Goertz | last post by:
Hi, consider the following program #include <iostream> #include <fstream> using namespace std; int main(int argc, char *argv){
12
3311
by: pekka | last post by:
I'm trying to measure user input time with my Timer class object. It isn't as easy as I expected. When using std::cin between timer start and stop, I get zero elapsed time. For some unknown reason, the clock seems to stop ticking during execution of std::cin. Here's my code: #include <ctime> #include <iostream> #include <string>
3
2896
by: Alex Snast | last post by:
hello guys I need to modify the std::cin delim char from the default ' ' and '\n' characters to ',' i know that i can edit the delim in the getline command however i'd like to know if there's something build in with me having to overload the std::cin operator thanks, Alex Snast.
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10410
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9984
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.