0

I am new to c# coding and am trying to write a little calculator type program that takes the numbers the user inputs and calculates them. What I have right now:

       int a;
        Console.WriteLine(" First Number? ");

        a = Console.ReadLine();

        int b;
        Console.WriteLine(" Second Number? ");

        b = Console.ReadLine();

        int sum = a + b; 

        Console.WriteLine("Answer: " + sum);

        Console.ReadLine(); 

Console.Readline() only works with strings and not integers, and I can't find what I can use instead. Can someone recommend something instead and general improvements on the code? Thanks.

  • 2
    Does this answer your question? [How can I convert String to Int?](https://stackoverflow.com/questions/1019793/how-can-i-convert-string-to-int) – misticos Mar 29 '20 at 09:40
  • Parse the `string` to an `int`: `a = int.Parse(Console.ReadLine())` or `int.TryParse(Console.ReadLine(), out a);` – Lee Stevens Mar 29 '20 at 09:41

1 Answers1

0

Every programming language has functions to convert strings to integer, so use assign the Console.ReadLine() result to a string and use such a conversion function to make an integer from it.

Marc Balmer
  • 1,780
  • 1
  • 11
  • 18