namespace GuestBook
{
internal class GuestTracker
{
public static (string fullName, int guestCount) NameLog()
{
int guestCount;
Console.Write("Welcome to the Cliffview Venue, " +
"Hubbard's Wedding check in, what is your first and last name? ");
string fullName = Console.ReadLine();
Console.Write($"\nHello {fullName} how many people will be joining you today? ");
string guestCountText = Console.ReadLine();
while (int.TryParse(guestCountText, out guestCount) == false)
{
Console.Write("\nSorry that wasn't a vaild number of guests, please entar a count using only the 0-9 characters: ");
guestCountText = Console.ReadLine();
}
Console.Write($"So we have a total of {guestCount} guests, is this correct? ");
string correctCount = Console.ReadLine();
** string no;
while((no = correctCount.ToLower()) != "yes" || no != "no")
{
Console.Write("\nI'm sorry, was that a yes or no? ");
correctCount = Console.ReadLine();
}
**
if(correctCount.ToLower() == "no")
{
Console.Clear();
Console.Write("\nOkay let's start over then.");
NameLog();
}
return (fullName, guestCount);
}
}
}
I was attempting to run the second while loop so that it stops when yes or no are entered into the correctCount
variable. When I take the ||
out and just have yes or no it works but as soon as I introduce the ||
it will loop infinitely.
I really only need the no response checked so I've modified my code, but the bug is bugging me. Why is it looping and not accepting "yes" or "no with the ||
?
Every string is either not equal to "yes" or not equal to "no". I.e., "yes" is not equal to "no", "no" is not equal to "yes", and any other string is not equal to both of them. You should be using the logical and (&&
) operator, not the logical or operation (||
):
while((no = correctCount.ToLower()) != "yes" && no != "no")
// Here ---------------------------------^