Quick question about nature of cin

Hi there, I'm writing simple program for my classes, that puts integers into vector, terminates this process with any non-number character, then prompts user for number and display sum of given number of integers from vector. BUT second cin simply doesn't work. I had a feeling that maybe cin.clear(); would help, but it didn't. Anyone can show me my mistake?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  int main()
{
		int a, b, sum=0;
		vector<int>numbers;
		cout << "please enter some numbers\n";
		while (cin >> a)
		{
			numbers.push_back(a);
		}
		cin >> b;
		for (int i = 0; i <= b; ++i)
		{
			sum += numbers[i];
		}
		cout << "sum of the numbers is " << sum << endl;
		system("pause");
		return 0;
	}
You need a cin.clear() followed by cin.ignore().
That is correct sir, thank you for you help. Would you mind me asking what exactly happened and why cin.clear and cin.ignore were required for the code to work properly?
When you entered a non-number character to get out the while loop, the cin stream went into a fail state. cin.clear() clears the fail flag, however, that non-number character is still in the stream (and that is what is automatically put into the second cin statement, seemingly "skipping over it"). We ignore that character using cin.ignore().
Last edited on
I am not sure if this code works but it seems like you are using int b to keep track of how many int the user have provided to the progeam, so I believe instead of having you b at the end of filling in vector process, you should first initilize the int b to 0 during declaration, then in your while loop where you are pushing back to vector you write b++.

and about having to terminate program without using non number char,
you can have a bool flag? use that bool flag in while loop, have all the codes you have in that loop, after your cout statement, change the bool flag according to your while loop. for ex, you started your bool as true, after cout have false and have while not false in while loop
1
2
3
4
5
cin >> b;
for (int i = 0; i <= b; ++i)
{
  sum += numbers[i];
}

Your computer isn't going to like this is b winds up being bigger than (or equal to) the size of the vector.

Since you are just pushing the numbers into the vector, there's no need to enter b. The vector keeps track of how many items are in it.

1
2
3
4
for (int i = 0; i < numbers.size(); i++)
{
  sum += numbers[i];
}

or
1
2
3
4
for (auto & num : numbers)
{
  sum += num;
}


As for why you need to use cin.clear() and cin.ignore():
cin.clear(): Arslan7041 explained it, but I want to add that subsequent calls to cin will fail unless you clear the fail state, even if you try to give valid input.
Technically speaking, cin.clear() will zero all the error bits, so cin.good() will return true.
You can also use ios::clear to set any of the iostate bits: http://www.cplusplus.com/reference/ios/ios/clear/

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'): If cin failed to get valid input, the invalid input is still left in the stream. You must extract and discard that invalid input.
It's good to note that cin.ignore() without any parameters will by default only ignore 1 character. I could enter ksjadgfka and only the first character would be ignored, the rest would remain.

Doing while (cin >> a) is fine, it will just keep looping as long as it gets valid input. Inputting a non-numeric character will make cin fail and the loop no longer execute. But you now have to clear the fail bit and take care of the remaining garbage input.
Topic archived. No new replies allowed.