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.
At first I thought that std::cin.clear() would sort that out, but it
didn't... So what does clear() do anyway, if not clear all cin data?
I looked it some places and saw that ignore is what I needed...
std::ignore(1000, '\n'); (or something bigger than 1000 characters).
However I don't like this very much... What if there were 10^100 other
characters in the input before the new line (this isn't possible I
guess but I'm just trying to explain why I don't like that way).
And in some examples I see the use of clear() but still I can't
understand what it does.
Thanks in advance,
cmad 5 12223
Chris Mantoulidis writes: 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.
At first I thought that std::cin.clear() would sort that out, but it didn't... So what does clear() do anyway, if not clear all cin data?
I looked it some places and saw that ignore is what I needed...
std::ignore(1000, '\n'); (or something bigger than 1000 characters).
However I don't like this very much... What if there were 10^100 other characters in the input before the new line (this isn't possible I guess but I'm just trying to explain why I don't like that way).
And in some examples I see the use of clear() but still I can't understand what it does.
Take a look at this. Although I warn in the message that it is from memory
no one objected to it, so it must have been right. http://makeashorterlink.com/?X13E228F6
In article <a8**************************@posting.google.com >,
Chris Mantoulidis <cm****@yahoo.com> wrote: At first I thought that std::cin.clear() would sort that out, but it didn't... So what does clear() do anyway, if not clear all cin data?
clear() resets the stream status flags. For example, when the stream
encounters an error (e.g. it wants to read an int and you give it
non-numeric characters), it sets a failure flag. Even after you skip over
the bad data with ignore() you can't continue reading until you reset the
flags with clear().
--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
On 6 Jan 2004 04:25:43 -0800, cm****@yahoo.com (Chris Mantoulidis)
wrote: 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.
At first I thought that std::cin.clear() would sort that out, but it didn't... So what does clear() do anyway, if not clear all cin data?
It clears the error state of the stream. If it's at eof(), or the last
operation failed (and set fail()), then you can call clear() to set
the state back to good() so that you can continue using the stream
(all operations fail when a stream isn't good()). I looked it some places and saw that ignore is what I needed...
std::ignore(1000, '\n'); (or something bigger than 1000 characters).
However I don't like this very much... What if there were 10^100 other characters in the input before the new line (this isn't possible I guess but I'm just trying to explain why I don't like that way).
I can see why you don't like that, but the correct code (by the new
2003 update to the standard) is
std::cin.ignore(std::numeric_limits<std::streamsiz e>::max(), '\n');
The extracts an unlimited amount of characters (the max is a sentinel
value). A bit verbose, but you can always write a little inline
function.
Tom
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
"Jon Bell" <jt*******@presby.edu> wrote in message news:bt**********@jtbell.presby.edu... In article <a8**************************@posting.google.com >, Chris Mantoulidis <cm****@yahoo.com> wrote: At first I thought that std::cin.clear() would sort that out, but it didn't... So what does clear() do anyway, if not clear all cin data?
clear() resets the stream status flags. For example, when the stream encounters an error (e.g. it wants to read an int and you give it non-numeric characters), it sets a failure flag. Even after you skip over the bad data with ignore() you can't continue reading until you reset the flags with clear().
Actually, you probably want to clear the errors before calling ignore. Ignore
could fail itself (such as hitting the end of file).
"Chris Mantoulidis" <cm****@yahoo.com> wrote in message
news:a8**************************@posting.google.c om... At first I thought that std::cin.clear() would sort that out, but it didn't... So what does clear() do anyway, if not clear all cin data?
Good question. Stream.clear() resets the error state of Stream. For
unfortunate historical reasons, Stream.clear (Foobit) has the effect of
resetting the error state to Foobit (and only Foobit), not of clearing
Foobit. Likewise, Stream.setstate (Foobit) does not actually set the error
state to Foobit, but logically adds the bit to the error state. These are
probably the most poorly named functions in C++. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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?
|
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...
|
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';
|
by: Johannes Meng |
last post by:
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...
|
by: Ralf Goertz |
last post by:
Hi,
consider the following program
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv){
|
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,...
|
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...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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,...
| |