I'm trying to create a shoe store program where the user will type in the SKU number to get the desired shoe. I'm trying to create an if statement that will ask the user for the quantity if the SKUs match or display "Enter a valid SKU: ", if it is invalid. Now it works but it automatically says that the number is invalid no matter what SKU I put in. Then when the entry is valid it no longer asks for the quantity!
while (keepBuyingShoes == true)
{
// ask user for sku and quantity
int quantity = 0;
Console.WriteLine("Enter shoe SKU: ");
String sku = Console.ReadLine();
if (sku != s1.SKU || sku != s2.SKU || sku != s3.SKU || sku != s4.SKU || sku != s5.SKU)
{
Console.WriteLine("That is an invalid SKU number!");
Console.WriteLine("Enter shoe SKU: ");
sku = Console.ReadLine();
}
else if (sku == s1.SKU || sku == s2.SKU || sku == s3.SKU || sku == s4.SKU || sku == s5.SKU)
{
Console.WriteLine("Enter quantity: ");
quantity = int.Parse(Console.ReadLine());
}
Did you notice how you use
||in bothifstatements despite how one is supposed to be the opposite of the other? Isn't that strange? Shouldn't at least one use&&in that case? Huh.So, yeah, as you might probably guess, your condition is wrong:
This is obvious if you actually think about the logic: you're essentially saying "if the SKU isn't
s1or isn'ts2or isn'ts3or isn'ts4or isn'ts5...". In order for this to be false, the SKU would have to be all of those at once, which is impossible. So, it's a tautology. No wonder it's always entering thatif.The solution is to use
&&instead:That way, you enter the
ifif the SKU is never any one of those.And then you can just turn the following
else ifinto a regularelse.Now, you should probably shove all those SKU objects into a collection and use things like
AnyandAll, but that's a topic for another time.