While loop being weird

I've written this loop I'm not sure if while was the best way to go about it but anyway... The problem is that it only half works.. If I enter "p" or "c" once it will still execute the while loop. However if I put p or c in a second time it will then return. If I put in anything else it will keep executing the while loop. What is going on here?


1
2
3
4
5
6
7
8
9
10
11
12
13
void add()
{
	string type;
	cout << "Permanent or Casual? (p/c)" << endl;
	cin >> type;
	while (type != "p" || type != "c"){  //maybe change to for?
		cout << "Invalid type. Please enter either p (permanent) or c (casual)" << endl;
		cin >> type;
		if(type == "p" || type == "c"){
			return;
		}
	}
}
Okay I changed line 6 to

while (!(type == "p" || type == "c"))

and it seems to be working now. I don't get why this would work but the other way wouldn't but whatever haha.

Edit: I'd love for someone to explain this to me if possible :)
Last edited on
Think of it like this:

Under what conditions would while (type != "p" || type != "c") be false?
If type does not equal p or if type does not equal c? :s Hmm it must be obvious but I still can't see the problem I feel silly lol.
Topic archived. No new replies allowed.