vector <int> a;              //first vector
    vector <int> b;               //second vector
    vector <int>::iterator i;        //iterator for first vector
    vector <int>::iterator i1;         //iterator for second vector


    //reading of first vector
    int temp;
    while(cin>>temp)
    {
        a.push_back(temp);

    }
    i=a.begin();
    while(i!=a.end())
    {
        cout<<*i<<" ";

        i++;
    }
    //end of reading first vector

    //reading of second vector
    int temp2;
        while(cin>>temp2)
    {
        b.push_back(temp2);

    }
    i1=b.begin();

    while(i1!=b.end())
    {
        cout<<*i1<<" ";

        i1++;
    }

    // end of second vector

i can read elements for 1st vector .however program doesnot go for second vector.

How do you determine the end of the input for vector a?

loop exits when i press any alphabet on kyeboard.

When you type in that letter at line 9 cin enters an error state because it can not complete the requested operation, input a number. Additionally the letter you typed is left in the input buffer.

This means that at line 25 cin is still in an error state because you have not cleared it so it automatically fails. If you did clear the error it would still not enter the loop because the letter is still in the input buffer and it would immediately fail again at the operation read a number and put cin back into an error state.

Just inputing a letter when your program is expecting a number is not a good way to indicate the end of data you need a better strategy for reading data from the using for example

  • Sentinal Value: the list is finished when the user inputs a specific sentinal value, 0 or -1 are common ones. Any other value gets put in the list
  • After each value has been input ask if there is another value
  • Before asking for any values ask the user how many values they wish to input
  • Read the input as text, if it is a string of digits convert to a number otherwise end the loop

There are probably other strategies those are just what I can think of off the top of my head.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.