Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7794801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:52:54+00:00 2026-06-01T22:52:54+00:00

I have two pieces of code.They work properly when it is used alone in

  • 0

I have two pieces of code.They work properly when it is used alone in the main().

vector<int> v;

cout << "Enter sequance of integers "<< "(press q to quit) : ";
istream_iterator<int> start_cin(cin);
istream_iterator<int> end_of_cin;
copy(start_cin,end_of_cin,back_inserter(v));

for ( vector<int>::iterator It = v.begin();It != v.end(); It++ )
    cout << *It << "\t";
cout << endl;

and

vector<string> vS;
cout << "Enter three strings : ";
for ( int i = 0; i < 3; i++ )
    vS.push_back(*istream_iterator<string>(cin));

ostream_iterator<string> sIt(cout,", ");
copy(vS.begin(),vS.end(),sIt);
cout << endl;

When these two part use together,i.e

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <string>
using namespace std;

int main ()
{
    // first part

    vector<int> v;

    cout << "Enter sequance of integers "<< "(press q to quit) : ";
    istream_iterator<int> start_cin(cin);
    istream_iterator<int> end_of_cin;
    copy(start_cin,end_of_cin,back_inserter(v));

    for ( vector<int>::iterator It = v.begin();It != v.end(); It++ )
        cout << *It << " \t";
    cout << endl;


    vector<string> vS;
    cout << "Enter three strings : ";
    for ( int i = 0; i < 3; i++ )
        vS.push_back(*istream_iterator<string>(cin));

    ostream_iterator<string> sIt(cout,", ");
    copy(vS.begin(),vS.end(),sIt);

    cout << endl;

    return 0;
}

here first part worked but second part give output: Enter Three Strings : , , ,.
I want to know that what is the reason behind this behaviour?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:52:55+00:00Added an answer on June 1, 2026 at 10:52 pm

    After the copy() has completed cin will be in an unreadable state (!cin.good()), due to the failed read of the “integer” q. This means the subsequent for loop will fail to read anything.

    Add:

    cin.clear();
    cin.ignore(); // To skip the unread "q"
    

    before the for loop.

    EDIT:

    As commented by James Kanze, check to ensure "q" was the cause of the termination of the copy():

    ...
    
    cin.clear();
    string int_read_terminator;
    cin >> int_read_terminator;
    if ("q" != int_read_terminator)
    {
        cerr << "Integer copy() failure: " << int_read_terminator << "\n";
    }
    else
    {
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two pieces of jquery code, they work perfectly separate, but together -
I have two pieces of code which are very similar in that they register
I have two pieces of C++ code running on 2 different cores. Both of
I have these two pieces of code, wich one is more readable? foreach decimal
I have written two pieces of code which I intended to have identical outputs,
i have the following two pieces of code which i think should be identical
I have two very similar pieces of ASP.NET code that send a file in
In two different views I have the following two pieces of code. FIRST <table
I have two pieces of code that I'm using to learn about multiprocessing in
I have the following two pieces of code: public class C { public void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.