some matter about cin

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...



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <vector>
using namespace std;

int main (void)
{
	vector<int> 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


Chervil, Andy, Catfish.....I'm very appreciate about your reply.

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

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:

1
2
3
4
5
#include <limits>

// ...

cin.ignore(numeric_limits<streamsize>::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)...


what about this?
1
2
3
4
5
6
7
ifstream infile("input.txt");
string line;
while (getline(infile, line))
{

  // ...
}


That a very similar idea, the loop terminates when the getline fails. It seems a common enough technique and I see nothing wrong with it.
Topic archived. No new replies allowed.