473,513 Members | 4,022 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use istream_iterator to read input from cin twice?

I have the following code:

vector<intA, B;
copy(istream_iterator<int>(cin), istream_iterator<int>(),
back_inserter(A));
copy(istream_iterator<int>(cin), istream_iterator<int>(),
back_inserter(B));

If I input the following

1 2 3 4 a <CR>

then I can get A as 1,2,3,4 which is OK, but then the second copy
won't read anything and the B becomes empty.

I can see that cin has bad bit set to true after the first copy which
it should be. But even after I called a cin.clear() before the second
copy, I still couldn't get the program to try to read my second line
of input into B. I guess somewhat you should tweak the cin to force
the second copy work. But how?

Thanks
Ou

Mar 22 '07 #1
3 4975
On 22 Mar 2007 13:49:48 -0700 in comp.lang.c++, ol*****@gmail.com wrote,
>I can see that cin has bad bit set to true after the first copy which
it should be. But even after I called a cin.clear() before the second
copy, I still couldn't get the program to try to read my second line
of input into B.
Even after you call cin.clear(), the funky character is still waiting to
be read in the input stream. You need to do something to read it. My
favorite would probably be
std::cin.ignore(std::numeric_limits<std::streamsiz e>::max(), '\n');

Mar 22 '07 #2
On Mar 22, 5:31 pm, David Harmon <sou...@netcom.comwrote:
On 22 Mar 2007 13:49:48 -0700 in comp.lang.c++, oliu...@gmail.com wrote,
I can see that cin has bad bit set to true after the first copy which
it should be. But even after I called a cin.clear() before the second
copy, I still couldn't get the program to try to read my second line
of input into B.

Even after you call cin.clear(), the funky character is still waiting to
be read in the input stream. You need to do something to read it. My
favorite would probably be
std::cin.ignore(std::numeric_limits<std::streamsiz e>::max(), '\n');
It works great, thanks a lot.

Mar 23 '07 #3
On Mar 23, 9:55 am, oliu...@gmail.com wrote:
On Mar 22, 5:31 pm, David Harmon <sou...@netcom.comwrote:
On 22 Mar 2007 13:49:48 -0700 in comp.lang.c++, oliu...@gmail.com wrote,
>I can see that cin has bad bit set to true after the first copy which
>it should be. But even after I called a cin.clear() before the second
>copy, I still couldn't get the program to try to read my second line
>of input into B.
Even after you call cin.clear(), the funky character is still waiting to
be read in the input stream. You need to do something to read it. My
favorite would probably be
std::cin.ignore(std::numeric_limits<std::streamsiz e>::max(), '\n');

It works great, thanks a lot.
By the way, for those who are interested in this post, here is a
working code:

vector<intA, B;
// Type 1 2 3 4 a <CR>
copy(istream_iterator<int>(cin), istream_iterator<int>(),
back_inserter(A));
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
// Type 4 5 6 7 b <CR>
copy(istream_iterator<int>(cin), istream_iterator<int>(),
back_inserter(B));

Mar 23 '07 #4

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

Similar topics

4
4141
by: Bill Rudolph | last post by:
The member function basic_ios::operator!() returns the bool result of the basic_ios::fail() function which is true if either failbit or badbit is set (This is per p. 616 of TC++PL by B. Stroustrup and p. 34 of Standard C++ IOStreams and Locales by Angelika Langer and Klaus Kreft). The implementation of the input stream iterator in the...
9
3166
by: Alex Vinokur | last post by:
------ foo.cpp : BEGIN ------ #include <cassert> #include <vector> #include <string> #include <iostream> #include <iterator> #include <fstream> using namespace std;
1
2552
by: Dave | last post by:
#include <iostream> #include <list> #include <string> using namespace std; int main() { list<string> the_list( (istream_iterator<string>(cin)),
0
4924
by: Richo11 | last post by:
This is probably a silly question but I am learning sorry... I am trying to read in a series of strings from standard input using the istream_iterator member function, how does istream_iterator<string>eos work. How does it know that you have finished entering data? :confused: any help appreciated. Cheers Richo
2
7039
by: Marcus Kwok | last post by:
I am writing a program to read in a file, do some processing, then write it out to a different file. I really like the idiom I use for output: // std::vector<std::string> vec; // std::ofstream out; std::copy(vec.begin(), vec.end(), std::ostream_iterator<std::string>(out, "\n"));
6
8467
by: George | last post by:
Hi All, I'm trying to learn c++/stl. I'd like a fancy way to read lines of an ascii file into vector of stringbufs. I made a first attempt, but the compiler complains about private constructors in streambuf. I'd like to use algorithms instead of a loop, but I don't know if that is possible. Any ideas? Thanks in advance.
8
3082
by: dragoncoder | last post by:
Hi, I am just a newbie in STL and trying to learn istream_iterator. My problem is I want a program which will take a comma separated string from the command line, tokenize it and copies into a vector. Then I am printing the vector. Here is the code I tried. #include <iostream> #include <sstream> #include <vector>
5
2805
by: Pradeep | last post by:
Hi All, I am facing some problem using istream_iterator for reading the contents of a file and copying it in a vector of strings.However the same thing works for a vector of integers. The code that doesn't work is std::vector<std::stringvecStr; std::ifstream fstrRead("Test.txt");
0
1317
by: thomas | last post by:
hi guys, I got a question about istream_iterator. ---code--- istream_iterator<intinput(cin), end; vector<intvec; copy(input, end, inserter(vec, vec.begin())); ---code--- the problem is that I don't want to copy all the data from input to
0
7270
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...
1
7125
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...
0
7543
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...
0
5703
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...
1
5102
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...
0
4757
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...
0
3252
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3239
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
813
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.