integer condtion in while loop

Bit of an odd request but can someone show me an example of a while loop in which the user gets asked just before for an integer input and if it isn't correct it will enter the while loop and loop until the right input is given... I just can't seem to do it :S Here's what I've done:

1
2
3
4
5
6
7
8
9
10
11
        std::cout << "\nHow do you wish to fight: ";
	std::cin >> fightType;
	std::cin.ignore();
	while(!std::cin && (fightType!=1 || fightType!=2)) { // wrong, change code <<<<<<
		std::cout << "--Invalid input--\n\n";
		std::cout << "How do you wish to fight: ";
		std::cin.clear();
		std::cin.sync();
		std::cin >> fightType;
		std::cin.ignore();
	}


If you enter a letter then it comes up with invalid input, so that works fine, but when you enter a number other than 1 or 2 it still works
A not-equal-to B OR A not-equal-to C is true unless A is both equal to B and C at the same time.
I feel like its right in front of me and I just can't see it!

A not-equal-to B OR B not-equal-to C
fightType not-equal-to 1 OR fightType not-equal-to 2

is true unless A is both equal to B and C at the same time
is true unless fightType is both equal to 1 and 2 at the same time.

But I can't think how to change it!
Let's see if I can re-explain this and hopefully lead you to an answer. :)

As it stands, lets consider the different possibilities.

fightType is 1:
Is it not equal to 1? Nope, it is 1.
Since we have an or, maybe the other condition is true; if that's so we can enter the loop.
Is it not equal to 2? Aha, it's 1, which isn't 2.
So we enter the loop.

fightType is 2:
Is it not equal to 1? Yep, it's a 2.
So we enter the loop.

fightType is 3,4,0, etc:
Is it not equal to 1? Yes.
We enter the loop.

Hope this helps.
Oh I get it! After reading this post about 20 times lol...

I was unsure on how to change that line so I decided to go for a different approach with this line:

(!std::cin && (fightType>0 && fightType<2))

But this doesn't work either. I don't know why I can't do this lol :S
while( std::cin reports an error OR ( the number the user entered is neither 1 nor 2 ) )

The last expression, said another way:

the number the user entered is not 1 and the number the user entered is not 2

[Edit: DeMorgan's rule can help with the boolean logic. DeMorgan's rule says that (using C++ operators):

a && b == !( !a || !b )

which can be re-written as

!( a && b ) == !a || !b
]
Last edited on
Is this it: ((!std::cin || fightType!=1) && (!std::cin || fightType!=2))
Yes, that works, however it is not the simplest expression.

It can be simplified to

1
2
3
while( !std::cin || ( fightType != 1 && fightType != 2 ) ) {
  // ...
}


Here is a link to a Wikipedia page on boolean algebra, which you might find useful.

http://en.wikipedia.org/wiki/Boolean_algebra_(introduction)

In particular, if you look at the section on Monotone Laws, the rule I applied to your
expression was Distributivity of v over ^.
And by further applying DeMorgan's to:
!std::cin || ( fightType != 1 && fightType != 2 )

you get:
!std::cin || !( fightType == 1 || fightType == 2 )

and doing it again, you get:
!(std::cin && (fightType == 1 || fightType == 2))

which is very close to what you started with intuitively.

But jsmith's version is more optimal for the machine, although is won't matter to a modern optimiser.
Last edited on
Thanks for the help guys! And I appreciate you not just giving me the answer! :)
Topic archived. No new replies allowed.