1

I code in C#, when I run code in visual studio code only the first console.WriteLine() statement will actually execute. I code in C# and I just installed visual studio code and the SDK for c#.

Heres the code I executed - a simple program to add 2 numbers

using System;

namespace MyApp 
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter number 1: ");
            int num1 = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter number 2: ");
            int num2 = int.Parse(Console.ReadLine());
            
        
        int sum = num1 + num2;
        Console.WriteLine(sum);
        Console.ReadKey();
        
        
        }
    }
}

The output is this:

Enter number 1:

Then when i enter a number it doesn't do anything else, like ask me what number 2 should be.

Jonathan
  • 685
  • 1
  • 10
  • 30
  • Are you entering valid integers, and no other characters? Seems to work fine on my machine. Any breakpoints set in the editor? – Jonathan Jul 28 '23 at 10:20
  • @Jonathan no breakpoints are used and im using integers between 1 and 10 – Jack Walker Jul 28 '23 at 10:22
  • Can you change [console](https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md#console-terminal-window) to a different value (like `externalTerminal`), and then check again ? – Luuk Jul 28 '23 at 10:26
  • Did you press Enter key after numeric value? – i486 Jul 28 '23 at 10:49
  • Note that you should almost always use `int.TryParse` rather than `int.Parse` – JonasH Jul 28 '23 at 12:40
  • @JonasH, whats the difference? i looked it up but im still a bit unsure. Its it just that tryparse will return a boolean value to determine success in converting? – Jack Walker Jul 29 '23 at 11:17
  • 1
    `int.Parse` will throw an Exception when it fails. (For example enter the text "number" in stead of a number ) – Luuk Jul 29 '23 at 11:39
  • If the user enters something other than a number you likely want to ask him/her to reenter it. So you put your tryParse into a loop that completes when the parsing succeeds. You do not want to use exceptions for this as it requires more code, and entering the incorrect number is not really an error or "exceptional. – JonasH Jul 31 '23 at 06:37

1 Answers1

1

The code runs fine on my machine.

Few things to check:

  1. Check if there are any breakpoints set in the editor by accident.
  2. Is the console closing after input or doing anything at all?
  3. Check this answer on another similar question (Use integratedTerminal).
  4. To run the code, use the terminal command dotnet run.

I'd also recommend using a try/catch to check if there's any formatting exceptions appearing. Example:

try
{
    Console.WriteLine("Enter number 1: ");
    int num1 = int.Parse(Console.ReadLine());

    Console.WriteLine("Enter number 2: ");
    int num2 = int.Parse(Console.ReadLine());


    int sum = num1 + num2;
    Console.WriteLine(sum);
    Console.ReadKey();
}
catch(FormatException ex)
{
    Console.WriteLine(ex);
    Console.ReadKey();
}

That way you can tell immediately if there's an error when running the code.

Jonathan
  • 685
  • 1
  • 10
  • 30
  • each time i execute the program i get this message come up a few times in the debug console, is this normal? Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. – Jack Walker Jul 28 '23 at 10:30
  • Can you check my updated answer and specifically step 3? – Jonathan Jul 28 '23 at 10:35
  • this generates the same problem as before, my output is still ' enter number 1: ', and then when i enter a number nothing happens – Jack Walker Jul 28 '23 at 10:36
  • Are you running the code via the terminal command `dotnet run` or a different way? – Jonathan Jul 28 '23 at 10:40
  • I was using dotnet build, dotnet run works. Sorry I'm new to this, thanks so much for the help. – Jack Walker Jul 28 '23 at 10:43
  • No worries! Remember, if you set the parameter according to step 3, you will have to change the window from "Debug Console" to "Terminal" in order to actually input anything. If you found the answer helpful and resolved the issue, feel free to mark as solved! – Jonathan Jul 28 '23 at 10:44