i'm trying initializing two vector<int> with the ""while(cin >> num)"" loop,the code as you see.

but i can't initializing ivec2, i don't know why...

 ``123456789101112131415161718192021222324252627282930313233`` ``````#include #include using namespace std; int main (void) { vector ivec1, ivec2; int num; //initializing ivec1 cout << "Enter the elements of ivec1, Q to stop"<< endl; while(cin >> num) { ivec1.push_back(num); } cin.sync(); //initializing ivec2 cout << "Enter the elements of ivec2, Q to stop" << endl; while(cin >> num) { ivec2.push_back(num); } //compare and output conclusion cout << "ivec1 and ivec2, "; if(ivec1 == ivec2) cout << "The are completely equal!!" << endl; else cout << "They are not equal!!" << endl; return 0; }``````

e...visual c++ 6.0

here is the excute output:
 ``` Enter the elements of ivec1, Q to stop 1 1 1 1 q Enter the elements of ivec2, Q to stop ivec1 and ivec2, They are not equal!! Press any key to continue ```

when i use "cin.clear()" to instead "cin.sync"...doesn't work!:
 ``` Enter the elements of ivec1, Q to stop 2 2 2 2 q Enter the elements of ivec2, Q to stop ivec1 and ivec2, They are not equal!! Press any key to continue ```

when i use "cin.ignore(1000, '\n')"....still doesn't work!
 ```Enter the elements of ivec1, Q to stop 3 3 3 3 q Enter the elements of ivec2, Q to stop ivec1 and ivec2, They are not equal!! Press any key to continue ```

-----------------------------------------------------------------

confusing......

is there another way to initializing vector at program executing time ?

better way?
Last edited on
`while(cin >> num)`
The end condition for this loop is when the fail flag is set for the cin stream.
Before you can do any futher input from the stream, you need to clear the flag(s).
add this before the second input:
`cin.clear();` to clear the error flags
and
`cin.ignore(1000, '\n');` to get rid of the unwanted input from the buffer.
Last edited on
On line 12, you're using operator>> to extract ints from cin. But 'Q' is not an int, so if you enter it at the command line then the extraction will fail, putting cin into a fail state. Once that has happened, all subsequents cin calls will fail.

The quickest solution would be to select an int value to use as the sentinal. Perhaps -1? And use that to terminate the loop.

If you want to use Q to quit the loop, then you will need to read strings and then convert the numbers from the ascii string reprentation to int yourself. Using failure to terminate a loop is not a good idea.

Andy
`cin.ignore(1000, '\n');`

Or if you want to impress others do this instead:

 ``12345`` ``````#include // ... cin.ignore(numeric_limits::max(), '\n');``````

Catfish4, I agree with you. That's a more correct and complete answer. But it's a lot more typing :)
But I don't think failure is a decent way terminate the loop (even though entering Q calls failure)...
Last edited on
 But I don't think failure is a decent way terminate the loop (even though entering Q calls failure)...

 ``1234567`` ``````ifstream infile("input.txt"); string line; while (getline(infile, line)) { // ... }``````