Do While loop breaks when typing charecter

I don't know if it is a bug or if I'm wrong but someone please help me. I encountered this problem with a bigger program of mine and narrowed it down to this problem...
This very simple works fine and like it is supposed to until you put a character in either one of the integrals???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #include <iostream>
using namespace std;

int main() {
	int test, test2;

	do {
		cout << "Test";
		cin >> test;
		cout << "Second test";
		cin >> test2;
	} while (test != 10);

	system("PAUSE");
	return 0;
}
A char is not an int - so when you attempt to extract an int from the input steam and a non-int is present, then the stream enters 'fail' mode. Before further input can be done the stream error needs to be cleared and the invalid input removed from the stream.

Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <limits>

int main()
{
	int n {};

	while ((std::cout << "Enter a number: ") && !(std::cin >> n)) {
		std::cout << "Not a number\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cout << "Number was " << n << '\n';
}


If the cin >> fails because of a non-number, the bool result is false so the not will be true so the while body will be entered. .clear() clears the error and .ignore() removes the remaining chars up to a new line - ready for new input. Hence the while loop will only terminate when a number has been entered.


Hello Leevi,

To expand.

Formatted input, i.e., cin >> test;, means that for whatever "test" is defined as, in this case an "int", it expects a whole number "int" to be entered and only a number. Any non number character will put "cin" in a failed state as seeplus has mentioned.

The other part of formatted input is that it will stop at a white space or new line whichever comes first. Leaving anything not used in the input buffer for the next input whatever that may be.

If formatted input is used with a string something like "first last" name will put "first" into the variable stopping at the space and leave "last" in the input buffer for the next input.

With, cin >>, you need to be aware of the variables type and what may be entered.

Sometimes you can use this to your advantage, but usually not.

The opposit is unformatted input, usually "std::getline()", but this only works for a "std::string".

The difference between formatted input and unformatted input is that formatted input will leave the new line in the input buffer and unformatted input t, usually "std::getline()", will take up to and including the new line, but it will discard the new line.

When the time comes and you mix formatted and unformatted input you will need to clear the input buffer after a formatted input before an unformatted input. If there are more than 1 formatted inputs you only need to clear the input buffer, the "cin.ignore(...)", after the last formatted input, but before an unformatted input.

Andy
Topic archived. No new replies allowed.