473,513 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems with cin (already tried getline)

Hi there.

I have some problems with cin. Here's the code:

http://rafb.net/p/GhK3AU65.html

If you press '6', and 'enter', it will have to ask for another insert of
a string. However, in this case, it goes straight without waiting for
input... any ideas?
Jul 19 '07 #1
9 1983
Michele 'xjp' wrote:
Hi there.

I have some problems with cin. Here's the code:

http://rafb.net/p/GhK3AU65.html

If you press '6', and 'enter', it will have to ask for another insert of
a string. However, in this case, it goes straight without waiting for
input... any ideas?
yes, there is a \n in the buffer. Put cin.sync() after cin >scelta,
and everything will work ;)

Regards,

Zeppe
Jul 19 '07 #2
On 2007-07-19 22:12, Michele 'xjp' wrote:
Hi there.

I have some problems with cin. Here's the code:

http://rafb.net/p/GhK3AU65.html
Please post the code inline.
If you press '6', and 'enter', it will have to ask for another insert of
a string. However, in this case, it goes straight without waiting for
input... any ideas?
Because the enter you pressed after the 6 is still in the input buffer,
so the getline() will read that, and since it's the terminator chosen
(it's default by the way, you don't have to specify it) it will read in
an empty string.

By the way, why oh why:

#define clrscr() printf("\e[2J")

What's wrong with an inline function or something like that, don't you
know that macros are evil?

--
Erik Wikström
Jul 19 '07 #3
Zeppe ha scritto:
Michele 'xjp' wrote:
>Hi there.

I have some problems with cin. Here's the code:

http://rafb.net/p/GhK3AU65.html

If you press '6', and 'enter', it will have to ask for another insert of
a string. However, in this case, it goes straight without waiting for
input... any ideas?

yes, there is a \n in the buffer. Put cin.sync() after cin >scelta,
and everything will work ;)

Regards,

Zeppe
I get the same problem with cin.sync()...

Solved with:
cin.ignore(INT_MAX, '\n' );

after the initially cin >scelta

Thank you!
Jul 20 '07 #4
On Thu, 19 Jul 2007 22:12:09 +0200, Michele 'xjp' wrote:
Hi there.

I have some problems with cin. Here's the code:

http://rafb.net/p/GhK3AU65.html

If you press '6', and 'enter', it will have to ask for another insert of
a string. However, in this case, it goes straight without waiting for
input... any ideas?
Always verify input data, and always verify the state of the input stream,
after each input.

--
Obnoxious User
Jul 20 '07 #5
On Jul 20, 12:03 am, Zeppe
<ze...@remove.all.this.long.comment.email.itwrot e:
Michele 'xjp' wrote:
yes, there is a \n in the buffer. Put cin.sync() after cin >scelta,
and everything will work ;)
cin is an input buffer; cin.sync() has no defined meaning.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 20 '07 #6
On Jul 20, 11:39 am, Obnoxious User <O...@127.0.0.1wrote:
On Thu, 19 Jul 2007 22:12:09 +0200, Michele 'xjp' wrote:
I have some problems with cin. Here's the code:
http://rafb.net/p/GhK3AU65.html
If you press '6', and 'enter', it will have to ask for another insert of
a string. However, in this case, it goes straight without waiting for
input... any ideas?
Always verify input data, and always verify the state of the
input stream, after each input.
I'll second that. And if you want line oriented input, always
use getline, then (if necessary) istringstream to parse it.
Something like:

std::string line ;
getline( std::cin, line ) ;
std::istringstream l( line ) ;
int number ;
l >number >std::ws ;
if ( ! l || l.get() != EOF ) {
// We got a problem, somewhere between the keyboard
// and the chair.
}

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 20 '07 #7
James Kanze wrote:
On Jul 20, 12:03 am, Zeppe
<ze...@remove.all.this.long.comment.email.itwrot e:
>Michele 'xjp' wrote:
>yes, there is a \n in the buffer. Put cin.sync() after cin >scelta,
and everything will work ;)

cin is an input buffer; cin.sync() has no defined meaning.
Strousrup doesn't quite agree with you, in 21.6.2 (third ed.): "Flushing
an istream is done using sync()."

Regards,

Zeppe
Jul 20 '07 #8
On Jul 20, 7:29 pm, Zeppe
<ze...@remove.all.this.long.comment.email.itwrot e:
James Kanze wrote:
On Jul 20, 12:03 am, Zeppe
<ze...@remove.all.this.long.comment.email.itwrot e:
Michele 'xjp' wrote:
yes, there is a \n in the buffer. Put cin.sync() after cin >scelta,
and everything will work ;)
cin is an input buffer; cin.sync() has no defined meaning.
Strousrup doesn't quite agree with you, in 21.6.2 (third ed.): "Flushing
an istream is done using sync()."
The standard says (for the effects of std::filebuf::sync):

If a put area exists, calls filebuf::overflow to write the
characters to the file. If a get area exists, the effect is
implementation-defined.

Given that the semantics of filebuf are defined in terms of
FILE*, by reference to the C standard, I would expect
std::filebuf::sync to have the same semantics as the C function
fflush (which is likely what Stroustrup is thinking of when he
wrote that). According to the C standard, the effects of
fflush( stream ) are:

If stream points to an output stream or an update stream in
which the most recent operation was not input, the fflush
function causes any unwritten data for that stream to be
delivered to the host environment to be written to the file;
otherwise, the behavior is undefined.

All of which is very logical when one considers the technical
meaning of the English word flush, which only applies to output.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 20 '07 #9
James Kanze wrote:
The standard says (for the effects of std::filebuf::sync):

If a put area exists, calls filebuf::overflow to write the
characters to the file. If a get area exists, the effect is
implementation-defined.
We are talking about the sync function in basic_istream.
For sync(), "my" standard says, at 27.6.1.3#36 (Unformatted input
functions):

Behaves as an unformatted input function (as described in 27.6.1.3,
paragraph 1), except that it does not count the number of characters
extracted and does not affect the value returned by subsequent calls to
gcount().
wrote that). According to the C standard, the effects of
fflush( stream ) are:

If stream points to an output stream or an update stream in
which the most recent operation was not input, the fflush
function causes any unwritten data for that stream to be
delivered to the host environment to be written to the file;
otherwise, the behavior is undefined.

All of which is very logical when one considers the technical
meaning of the English word flush, which only applies to output.
I know the semantics of fflush in C, and I agree that it's logical and
that it agrees to the English language, but I can't get your point:
fflush is one thing (and there is the corresponding flush method in C++
for ostreams), and sync is another thing (with a quite different English
meaning, too) :)

Regards,

Zeppe
Jul 21 '07 #10

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

Similar topics

1
2350
by: Jim Phelps | last post by:
Hello all, I am in a bit of a pickle using the getline function with an ifstream. It does not seem to work as advertised. Here is my scenario. In a nutshell, my code needs to pick up a fixed...
6
1658
by: Fred H | last post by:
Hi I'm trying to write a more or less fool proof method of getting lines of text from either standard input (cin) or files (ifstream). It needs to be fool proof for later use, since I can be...
6
7136
by: Simon Gibson | last post by:
Hi there, im trying to write a program where you can write reports and save them into an array. im having problems with getting the string into an array tho it seems to be skipping over the...
7
2385
by: Jonathan | last post by:
Hey everyone! I am pretty new to C++, and I am just playing around with some things that I have learned so far (trying to put them to use before I continue on with the book). In one section of my...
11
2245
by: Christian Christmann | last post by:
Hi, I want to read a file line by line and tokenize the content. My code: ifstream Configuration; Configuration.open("config.cfg"); // assume that max. length of line in configuration file...
3
1774
by: Eric Lilja | last post by:
Hello, I'm creating a small utility for an online game. It involves parsing a text file of "tradesskill recipes" and inserting these recipes in a gui tree widget (similar to gui file browsers if...
4
2152
by: interpim | last post by:
ok the function is supposed to accept only non-negative integers... but it still accepts letters input through the function. Such as '23e' still passes through. I tried to use isalpha in the or...
1
5371
by: electrixnow | last post by:
Help!, I need to compile this code with static libs so it run on another XP machine that does'nt have MS Studio installed. When I compile now I get an ERROR: 1>------ Rebuild All started:...
10
5172
by: Terry IT | last post by:
hi, i'm using code like this string s while(getline(cin,s)){ process(s); } // this is the last line process(s);
0
7267
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
7391
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,...
0
7542
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
5697
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
4754
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...
0
3247
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3235
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1609
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.