I want my while loop to stop whenever the user enters 1 or 2. If anything else is entered then it should repeat. I tried putting an exclamation mark before the variable name and using an equality sign. I tried using a single or operator.
The petId variable is getting the right numbers — I see the NSLog()s for “Dog” and “Cat”. Why is the loop looping despite having a 1 or 2 entered?
int petId;
while(petId != 1 || petId!=2)
{
NSLog(@"%d",petId);
NSLog(@"Would you like to create a dog or a cat?\n1.Dog 2.Cat");
//Scan user input
scanf("%i",&petId);
if(petId == 1)
{
NSLog(@"Dog");
}
else if(petId==2)
{
NSLog(@"Cat");
}
else
{
NSLog(@"Invalid Entry");
}
}
You probably made a mistake in the boolean operators:
Replace
With