+ 1

How do I use strings with user input in C# without encountering a cannot convert error?

I was working on a C# calculator, and as I was testing the input, I encountered cannot convert string to int and int to string.

13th May 2022, 4:27 AM
Ethan J
Ethan J - avatar
3 Answers
+ 2
Random Coder int a = Convert.ToInt32(Console.ReadLine()); String op = Console.ReadLine(); int b = Convert.ToInt32(Console.ReadLine());
13th May 2022, 5:48 AM
AÍąJ
AÍąJ - avatar
+ 1
if you are a novice programmer, And you've been challenged in conversionsی AͱJ answer is the best way. In C#, there are two basic types of type conversion: Implicit Type Conversions Explicit Type Conversions On the opposite side, there is another error. This type of error occurs when user inputs are null or contain non-numerical characters. The answer is dependent on your code which is developed for WinForms, WPF, web applications, etc., Try-catch, regex, and checking all input characters could help you manage user code. Asp.net and WPF have the most straightforward way to solve the problem In WinForms, I think you can use Mask for textBox to accept numeric characters.
13th May 2022, 7:19 AM
hossein B
hossein B - avatar
+ 1
AJ & hossein B, Thanks for the solution and explanation of conversions.
14th May 2022, 3:30 AM
Ethan J
Ethan J - avatar