-4

That's the code. What happens is the console prints out

Enter a number(a) :

2

Enter a number(b) :

Enter a number(c) :

a + b + c =73 

Press any key to continue...

P.S - This is my solution to an exercise I had. The exercise tells me to enter 3 values from the console using integrals.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter a number(a) : ");
        int a = Console.Read();
        Console.WriteLine("Enter a number(b) : ");
        int b = Console.Read();
        Console.WriteLine("Enter a number(c) : ");
        int c = Console.Read();
        Console.WriteLine("a + b + c =" + (a + b + c));
    }
}
MatSnow
  • 6,657
  • 3
  • 16
  • 30
  • 2
    write `Console.ReadLine();` at the bottom. – Mustafa Çil Nov 17 '17 at 12:28
  • https://stackoverflow.com/questions/6825943/difference-between-console-read-and-console-readline – Wiktor Zychla Nov 17 '17 at 12:28
  • Oh! Thanks! Decided to place it here because I asked a ton of friends, not being able to answer it! Cheers! – user8909695 Nov 17 '17 at 12:29
  • But then comes another question. How come 2 + null + null = 73? – user8909695 Nov 17 '17 at 12:30
  • 1
    Another issue: Console.Read does not return the integer you entered. It looks at that character (e.g. '2') and returns the value that internally represents that character. See the [documentation](https://msdn.microsoft.com/en-us/library/system.console.read(v=vs.110).aspx). You'll want to parse the input. See [here](https://msdn.microsoft.com/en-us/library/b3h1hf19(v=vs.110).aspx) for the documentation. – S.L. Barth Nov 17 '17 at 12:31
  • @user8909695 change `Console.Read()`; to `Convert.ToInt32(Console.ReadLine());` – Mustafa Çil Nov 17 '17 at 12:42

3 Answers3

0

Add Console.ReadLine(); as the last line of the Main function

0

After your program does the output Console.WriteLine("a + b + c =" + (a + b + c)); your program ends and the consolewindow will close. Yout can call Console.ReadKey(); at the end, so the programm waits for another input from the user before its ending and closing the window

Olli
  • 590
  • 5
  • 19
0

You can use ctr+F5 to start without debugging, this will hold the screen. Alternatively you can add Console.ReadLine(); or Console.ReadKey(); at the very end of your code.

Danial Ahmed
  • 538
  • 6
  • 16