I created an ordinary console calculator. I want to, after performing any function, the program allows you to perform another, thereby not closing.

`string again = "yes"; while (again == "yes") { double a; double b; double total; char oper; Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("MathCalculate is working"); Console.ResetColor(); Console.WriteLine("\nEnter first number:"); a = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Enter operator:"); oper = Convert.ToChar(Console.ReadLine()); Console.WriteLine("Enter second number:"); b = Convert.ToDouble(Console.ReadLine()); if (oper == '+') { total = a + b; Console.WriteLine("Addition " + a + " and " + b + " equals: " + total + "."); } else if (oper == '-') { total = a - b; Console.WriteLine("The difference of the numbers " + a + " and " + b + " equals: " + total + "."); } else if (oper == '*') { total = a * b; Console.WriteLine("Multiplication of numbers " + a + " on " + b + " equals: " + total + "."); } else if (oper == '/') { total = a / b; Console.WriteLine("Division of numbers " + a + " on " + b + " equals: " + total + "."); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Error"); Console.ResetColor(); } Console.WriteLine("Do you want to continue work with calculator? (yes/no)"); again = Convert.ToString(Console.ReadLine()); } }` 
  • it could have been a little simpler, but in this form there should be no problem if we didn’t mess it up with letters that have the same display in different layouts, for example e in the word yes - rdorn

1 answer 1

At the end, you request permission to continue the cycle, and thus, regardless of the answer, you rewrite the variable again , which is responsible for your infinite loop. If you make a mistake even for one character, then the period will be different.

Console.ReadLine () immediately returns a string and you do not need to convert it to a string again.

I propose to make the following option.

 while (true) { /* * Ваша логика */ var answer = Console.ReadLine(); if(answer?.ToLower() == "no") break; }