Good afternoon, everybody, I teach C # and try to understand what the error is.

namespace OWKiller { class Program { static void Main(string[] args) { int processnum = 0; Console.WriteLine("CSGO #1 Overwatch #2"); Console.WriteLine(processnum); processnum = Console.Read(); Console.WriteLine(processnum); Console.ReadKey(); // { // foreach (var process in Process.GetProcessesByName("Overwatch")) // { /// process.Kill(); // } // } // } } } } 

If I write in console 1 then the compiler shows that I entered 49 and if 2 then the compiler shows 50, what is the reason? why instead of 1 and 2 it shows 49 and 50?

  • The code of the entered character is Grundy
  • @Grundy more detail you can? Are you coding about? - Win
  • c # takes console input as a string data type. And displays you the character code from the ASCII table (or equivalent). To get an int data type do a data type conversion - HegoJune

3 answers 3

Console.Read returns a number.

To get the entered character in the form of char, you can use the method Convert.ToChar

 ch = Convert.ToChar(Console.Read()); 
  • He seems to be trying to get a number. ch = Convert.ToInt32(Console.Read()); - HegoJune
  • @HegoJune, that’s exactly what it is, so the variable I’ve named is not processnum as the author of the question, but ch . You missed a call to ToChar . Console.Read and so returns Int32 - so it makes no sense to use Convert.ToInt32 result will be equivalent to Console.Read() - Grundy
  • Thanks for helping @Grundy - Win

Achieved the result with the following code.

 namespace OWKiller { class Program { static void Main(string[] args) { int processnum = 0; Console.WriteLine("Overwatch #1 CSGO #2"); Console.WriteLine(processnum); bool checked1 = int.TryParse(Console.ReadLine(), out processnum); if (checked1) { Console.WriteLine(processnum); } if (processnum == 1) { { foreach (var process in Process.GetProcessesByName("Overwatch")) { Console.WriteLine("Crashing Overwatch..."); process.Kill(); } } } else { foreach (var process in Process.GetProcessesByName("csgo")) { Console.WriteLine("Crashing CSGO"); process.Kill(); } } } } } 

Thank you all very much for your help!

  • Notice that char is not a number. And if you want to use the received symbol 1 as a digit - you need to call additionally Convert.ToInt32 for example - Grundy
  • @Grundy already understood, thanks! - Win
  int processnum = 0; Console.WriteLine("CSGO #1 Overwatch #2"); Console.WriteLine(processnum); // сделайте так чтобы проверить правильно ли вел пользователь значение bool isNumber = int.TryParse(Console.ReadLine(), out processnum ); if(isNumber){ Console.WriteLine(processnum); } Console.ReadKey();