473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

cin >> c

I'm writing a simple Unix console app. I need to take an input of a
single character from the user and discard the rest.

If I have:
char c;
cin >> c;
And the user types the letter 'a', then presses Enter, 'a' gets put into c
as I want. BUT, Enter remains as the next character in the stream, so if
I then do a getline(cin, word), I get an empty string in word. The other
requirement I need is that if the user just presses Enter without entering
a letter, c should be assigned to '\n'.

Ideally, it should process the character (whatever it is) without the user
having to press Enter.

--
Kevin W :-)
Opera/CSS/webdev blog: http://www.exclipy.com/
Using Opera: http://www.opera.com/m2/
Jul 22 '05 #1
7 2227
"Kevin W." wrote:

I'm writing a simple Unix console app. I need to take an input of a
single character from the user and discard the rest.

If I have:
char c;
cin >> c;
And the user types the letter 'a', then presses Enter, 'a' gets put into c
as I want. BUT, Enter remains as the next character in the stream, so if
I then do a getline(cin, word), I get an empty string in word. The other
requirement I need is that if the user just presses Enter without entering
a letter, c should be assigned to '\n'.
The simplest thing to fulfill your requirement is to use getline
for entering that single character too. Just use the first character
of the read line and be done with it. If what you read is an empty
line, then just assume 'c' was pressed.

Doing the above is often one of the simplest solutions to most
input problems:

just read a complete line as a string and use from that line
whatever you need. This also may mean to convert things on your
own or to extract the things you need from the input line as
required.

Ideally, it should process the character (whatever it is) without the user
having to press Enter.


Not possible with standard C++

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
Kevin W. wrote:
Ideally, it should process the character (whatever it is) without the
user having to press Enter.


This would require changing the attributes of the input device. Something
like 'raw' input or disabling canonical mode input processing, which would be
arranged using ioctl or stty or some such. I think you would first have to
find out what device you are receiving your standard input from. It's a while
since I used UNIX so I can't be very specific I'm afraid. You should do
better asking in a UNIX newsgroup.

--
Regards,

Joe Hotchkiss,
http://joe.hotchkiss.com

XXXXXXXXXXXXXXXXXXXXXXXXX
X joe.hotchkiss X
X at baesystems.com X
XXXXXXXXXXXXXXXXXXXXXXXXX

Jul 22 '05 #3

"Kevin W." <co*****@in.sig> wrote in message
news:op**************@localhost.localdomain...
I'm writing a simple Unix console app. I need to take an input of a
single character from the user and discard the rest.

If I have:
char c;
cin >> c;
And the user types the letter 'a', then presses Enter, 'a' gets put into c
as I want. BUT, Enter remains as the next character in the stream, so if
I then do a getline(cin, word), I get an empty string in word. The other
requirement I need is that if the user just presses Enter without entering
a letter, c should be assigned to '\n'.
char c(0);
std::string word;
std::getline(std::cin, word);
c = (word + '\n')[0];
Ideally, it should process the character (whatever it is) without the user
having to press Enter.


Not possible with standard C++.

-Mike
Jul 22 '05 #4
Using Windows Specific libraries can be done like this:

#include <iostream>
#include <string>
#include <conio.h>

void main( void )
{
std::string strg = "";
for(char ch = 0; ch != ' '; ch = _getch(),strg += ch);
std::cout << strg << std::endl;
}
"Kevin W." <co*****@in.sig> wrote in message news:<op**************@localhost.localdomain>...
I'm writing a simple Unix console app. I need to take an input of a
single character from the user and discard the rest.

If I have:
char c;
cin >> c;
And the user types the letter 'a', then presses Enter, 'a' gets put into c
as I want. BUT, Enter remains as the next character in the stream, so if
I then do a getline(cin, word), I get an empty string in word. The other
requirement I need is that if the user just presses Enter without entering
a letter, c should be assigned to '\n'.

Ideally, it should process the character (whatever it is) without the user
having to press Enter.

Jul 22 '05 #5
> The simplest thing to fulfill your requirement is to use getline
Just use the first character of the read line and be done with it.


Thanks.

--
Kevin W :-)
Opera/CSS/webdev blog: http://www.exclipy.com/
Using Opera: http://www.opera.com/m2/
Jul 22 '05 #6
Try doing this:
char c;
cin>>c;
cin.ignore();
cin.getline(name, int);

or, if you want to just capture one character, without the user having
to press enter:
(you must #include <conio.h>)
char c = getch();
cin.getline(name, int);

Hope i've been of some help.
Kieran

"Kevin W." <co*****@in.sig> wrote in message news:<op**************@localhost.localdomain>...
I'm writing a simple Unix console app. I need to take an input of a
single character from the user and discard the rest.

If I have:
char c;
cin >> c;
And the user types the letter 'a', then presses Enter, 'a' gets put into c
as I want. BUT, Enter remains as the next character in the stream, so if
I then do a getline(cin, word), I get an empty string in word. The other
requirement I need is that if the user just presses Enter without entering
a letter, c should be assigned to '\n'.

Ideally, it should process the character (whatever it is) without the user
having to press Enter.

Jul 22 '05 #7

"K Campbell" <ki****@cyrocom.co.uk> wrote in message
news:85**************************@posting.google.c om...
Try doing this:
char c;
cin>>c;
cin.ignore();
This will extract and discard the first of possibly
several characters following the one that gets extracted
and stored in 'c'. E.g. if I input "abc", character 'a'
gets put into 'c', character 'b' is thrown away, and character
'c' remains in the stream. You can give an argument of
'std::numeric_limits<std::streamsize>::max()' to 'ignore()',
and it will ignore all pending characters, not just the first.
cin.getline(name, int);

or, if you want to just capture one character, without the user having
to press enter:
(you must #include <conio.h>)
Not a standard C++ header. Please don't post such things here.
char c = getch();
Not a standard C++ function. Please don't post such things here.

cin.getline(name, int);

Hope i've been of some help.


Hmm.

-Mike
Jul 22 '05 #8

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

Similar topics

9
by: Francesco Moi | last post by:
Hello. I'm trying to build a RSS feed for my website. It starts: ----------------//--------------------- <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE rss PUBLIC "-//Netscape...
1
by: Christian Schmidbauer | last post by:
Hello! I prepare my XML document like this way: ------------------------------------------------------- PrintWriter writer; Document domDocument; Element domElement; // Root tag
2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
2
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script...
0
by: Arne Schirmacher | last post by:
I want to display a MySQL database field that can contain HTML markup. If I use <esql:get-string> then I get all of the database field, but all tags are escaped which is not what I want. If I use...
34
by: Mark Moore | last post by:
It looks like there's a pretty serious CSS bug in IE6 (v6.0.2800.1106). The HTML below is validated STRICT HTML 4.01 and renders as I would expect in Opera, FrontPage, and Netscape. For some...
11
by: Les Paul | last post by:
I'm trying to design an HTML page that can edit itself. In essence, it's just like a Wiki page, but my own very simple version. It's a page full of plain old HTML content, and then at the bottom,...
2
by: bissatch | last post by:
Hi, I am currently writing a simple PHP program that uses an XML file to output rows for a 'Whats New' page. Once written, I will only require updating the XML file and any pages that use the...
0
by: vdex42 | last post by:
Apologies if this has been asked before, but I haven't been able to find the answer to this yet: My problem is that .NET will not allow me to insert escaped '>' characters (i.e. &gt;) within the...
5
by: John Nagle | last post by:
This, which is from a real web site, went into BeautifulSoup: <param name="movie" value="/images/offersBanners/sw04.swf?binfot=We offer fantastic rates for selected weeks or days!!&blinkt=Click...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...
0
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...

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.