blah blah blah is here! blah blah » Close

up1down
link

I want to check whether a a person has entered "y" or "Y" on a console screen.

I'm trying to use:

string answer;
do
{
Console.WriteLine("Please enter the actors name");
tempActorList.Add(Console.ReadLine());
Console.WriteLine("Do you want to add another actor?");
answer = Console.ReadLine();
}
while ((answer = "Y" || "y"));

but apparently the || cannot be used on strings.

Can anyone suggest something that might work instead? Hopefully something that won't over inflate my code.

last answered one year ago

1 answers

up1down
link

When comparing variables in C#, you need to use the compare operator '==' rather than '='

Also, I would suggest reforming your code as below:

do
{
Console.WriteLine("Please enter the actors name");
tempActorList.Add(Console.ReadLine());
Console.WriteLine("Do you want to add another actor?");
answer = Console.ReadLine();
}
while ((answer.Trim().ToLower() == "y"));

vulpes
17279

Although foamy's solution is best, I'd just add that your original approach would have worked if you'd changed it to: while(answer == "Y" || answer == "y") as you'd then be OR'ing two boolean expressions.

Feedback