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.

8
  • Are you entering valid integers, and no other characters? Seems to work fine on my machine. Any breakpoints set in the editor? Commented Jul 28, 2023 at 10:20
  • @Jonathan no breakpoints are used and im using integers between 1 and 10 Commented Jul 28, 2023 at 10:22
  • Can you change console to a different value (like externalTerminal), and then check again ? Commented Jul 28, 2023 at 10:26
  • Did you press Enter key after numeric value? Commented Jul 28, 2023 at 10:49
  • 1
    int.Parse will throw an Exception when it fails. (For example enter the text "number" in stead of a number ) Commented Jul 29, 2023 at 11:39

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
Can you check my updated answer and specifically step 3?
this generates the same problem as before, my output is still ' enter number 1: ', and then when i enter a number nothing happens
Are you running the code via the terminal command dotnet run or a different way?
I was using dotnet build, dotnet run works. Sorry I'm new to this, thanks so much for the help.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.