I am working on a c# console dice sim lab for my class in Visual Studio 2015. I broke my program to three if loops depending on the user's response. I have one dedicated as a do while loop if the user doesn't input a valid response. I am stuck in that loop for some reason and can't get out. On top of that, since it's to tell the user to input a valid response, it's the first if statement. Because of that, even if I input "y", "Y", "n", or "N" it still initializes. Here is the part in question.
// get response from user
response = ReadLine();
// loop starts if user does not reply with valid response in order to retrieve a valid response
if (response != "N" || response != "Y" || response != "n" || response != "y")
{
do
{
WriteLine("Please reply with Y or N");
response = ReadLine();
}
while (response != "N" || response != "Y" || response != "n" || response != "y");
}
I am using the or operator, so I don't understand why it's looping the way it is.
response != "N" || response != "Y" || response != "n" || response != "y"
should be
response != "N" && response != "Y" && response != "n" && response != "y"
Because you should quit the loop if you hit one of the valid responses
You need to use && instead of ||
response != "N" && response != "Y" && response != "n" && response != "y"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With