0

I am starting to learn c# and I have a simple program to request two numbers from the user and add them together. When I run my program , I get asked for the first number , I type it in and that's it, the program doesn't go any further. I am running it in visual studio

Can anyone tell me what I'm doing wrong?

using System;

namespace Coding
{
    class Program
    {
        static void Main(string[] args)
        {

            int number1;
            int number2;
            int result;


            Console.WriteLine("Enter first  number to be calculated");
            number1 = Convert.ToInt32(Console.ReadLine());


            Console.WriteLine("Enter second  number to be calculated");
            number2 = Convert.ToInt32(Console.ReadLine());

            result = number1 + number2;
            Console.WriteLine("The Total is" + " " + result);
        }
    }
}

result screenshot enter image description here

johndoe12345
  • 291
  • 2
  • 4
  • 13
  • 3
    You need to press enter after entering the number, it's reading a line – steve16351 Jun 06 '19 at 13:26
  • 4
    Most likely the program ends and shows the results properly. You just don't get to see it. Try adding `Console.ReadKey();` at the end. – Bartłomiej Popielarz Jun 06 '19 at 13:26
  • 2
    Side note: add `Console.ReadLine()` (or `Console.ReadKey()`) after the last `Console.WriteLine` in order to *pause execution* before exit so that you can look at the output. – Dmitry Bychenko Jun 06 '19 at 13:27
  • 2
    @BartłomiejPopielarz first thought, but there is a second `ReadLine()` and OP said it stops/hangs/crashes at/after the first one – René Vogt Jun 06 '19 at 13:27
  • 1
    @RenéVogt That's not what they say. It sounds like they're expecting to press a number and immediately be asked for the second number, without having to press enter. Once they resolve that issue (if that is the issue) then Bart's comment *needs* to be taken into consideration or the next question will be *"why does my application close without showing the result?"* – Reinstate Monica Cellio Jun 06 '19 at 13:29
  • I get asked for first number, i type it in and press enter and it shows the number on the screen and the program doesnt go any further. I dont get asked for the second one – johndoe12345 Jun 06 '19 at 13:30
  • 1
    Can't reproduce, which number exactly do you enter? How does "program doesnt go any further" looks like? Can you add a screenshot? – Sinatr Jun 06 '19 at 13:31
  • 1
    Debug the input coming from Console.Readline() there might be a space – André Sanson Jun 06 '19 at 13:31
  • @AndréSanson That still would not explain described behavior. – Fildor Jun 06 '19 at 13:33
  • i added screenshot – johndoe12345 Jun 06 '19 at 13:33
  • Oh, so you are **not** in Visual Studio but in Visual Studio **Code** ... – Fildor Jun 06 '19 at 13:35
  • Never seen such console. Mac? Net .Core? – Sinatr Jun 06 '19 at 13:35
  • @Sinatr that's VS Code Editor with .Net Core – Fildor Jun 06 '19 at 13:36
  • .net sdk. Where should i run it from so ? I am doing a pluralsight training course and the trainer said to run it this way – johndoe12345 Jun 06 '19 at 13:36
  • If you would create Console App (.Net Framework) using Visual Studio 2017 (or other version) it would work as it was to me. No experience with VS Code. [edit] question tags and wait until someone who has helps. – Sinatr Jun 06 '19 at 13:39
  • 2
    As far as we can tell the code you posted is proper and should run just fine, it is likely an issue with the commandline that you are using. If possible try running the built executable file without visual studio. – Rhaokiel Jun 06 '19 at 13:40
  • there is no .exe, downloading visual studio now and will try run it again – johndoe12345 Jun 06 '19 at 13:42
  • ok sorted. in studio code you need to type dotnet restore and then dotnet run and it works fine, thanks everyone – johndoe12345 Jun 06 '19 at 13:57

0 Answers0