1

I've got a Console.ReadLine() inside a finite for loop that never ends reading.
I am using VS Code on Linux Mint. I execute by pressing F5.

using System;

class Person
{
    public string Name { get; set;}
    public override string ToString()
    {
        return "My name is " + Name;
    }
}

class Program
{
    static void Main(string[] args)
    {
        int n = 3;
        Person[] p = new Person[n];

        for (int i = 0; i < n; i++)
        {
            p[i] = new Person()
            {
                Name = Console.ReadLine()
            };
            Console.WriteLine("I just read " + p[i]);
        }

        for (int i = 0; i < n; i++)
        {
            Console.WriteLine(p[i].ToString());
        }
    }
}

I expected to input three names and then output them.
I input by typing a name and then pressing Enter.
The issue is that I can keep inputting forever and that Console.WriteLine("I just read " + p[i]); never gets executed. This happens in the Debug Console.

Jorje12
  • 415
  • 1
  • 3
  • 14
  • You could be getting an exception and exiting program. – jdweng May 17 '21 at 13:22
  • Can you add a message after reading the line, or a breakpoint, just to see if it's actually reading your input? – Martheen May 17 '21 at 13:27
  • @Martheen Added `Console.WriteLine("I just read " + p[i]);` after `p[i] = new Person(Console.ReadLine());` and it doesn't output anything. – Jorje12 May 17 '21 at 13:29
  • 2
    I think you're supposed to type on Terminal instead of Debug Console – Martheen May 17 '21 at 13:33
  • Did you try adding a step (`string name = Console.ReadLine(); p[i] = new Person{Name = name};`)? With a breakpoint to see if `name` is correctly assigned? – Rafalon May 17 '21 at 13:34
  • @Martheen The terminal has this: `Terminal will be reused by tasks, press any key to close it.` – Jorje12 May 17 '21 at 13:36
  • That's the Task Build. Switch it to the other one with the name of your app – Martheen May 17 '21 at 13:38
  • @Martheen That's the only Terminal active. I think this has to do with the .NET SDK and VSC because I keep getting the following message in the OUTPUT tab: `It was not possible to find any installed .NET SDKs.`. I think something went wrong when I've installed the SDK. – Jorje12 May 17 '21 at 13:40
  • You should add that to your question. – Martheen May 17 '21 at 13:42
  • @Martheen Thank you. I have seen a lot of similar if not identical issues on https://github.com/dotnet/core and I plan on opening an issue there. I still don't think I deserved all the downvotes. Thank you for your help. – Jorje12 May 17 '21 at 13:45

2 Answers2

0

I can't reproduce this on Windows or Ubuntu. My guess is that Mint buffers the output.

Try adding Console.Out.Flush(); after Console.WriteLine("I just read " + p[i]);

Hans Kilian
  • 18,948
  • 1
  • 26
  • 35
  • `Console.Out.Flush();` does nothing. VSC terminal closes on its own. I am pretty sure that the problem comes from OmniSharp. I keep getting `It was not possible to find any installed .NET SDKs` messages. Even though I've installed the `dotnet SDK` per these instructions: https://learn.microsoft.com/en-us/dotnet/core/install/linux-snap . I've then went on https://github.com/dotnet/core and I've found many similar/identical issues. – Jorje12 May 18 '21 at 09:34
  • Can you run the program from a terminal or shell using `dotnet run`? – Hans Kilian May 18 '21 at 09:45
  • Yes, I can. And that works. What just happened ? I ran the program like on Windows, by pressing F5. – Jorje12 May 18 '21 at 09:54
  • Maybe running it from the shell built an executable that you didn't have before. And now when you press F5 it just runs the executable? Maybe it's building from inside VSCode that doesn't work? – Hans Kilian May 18 '21 at 09:58
  • Your problem most likely has something to do with the error you get where it complains about not being able to find the SDK. What does `dotnet --list-sdks` give as output if you run it? – Hans Kilian May 18 '21 at 10:03
  • 5.0.203 [/snap/dotnet-sdk/126/sdk] – Jorje12 May 18 '21 at 10:14
  • In your .csproj file, which framework does it target? It should be `net5.0`. If that's as it should be, then I think I would try reinstalling VSCode if it were me. – Hans Kilian May 18 '21 at 10:29
  • So it's: `net5.0` – Jorje12 May 18 '21 at 11:31
0

This is still an issue on Linux. Running Visual Studio Code, in debug mode. Output is being directed to Debug Console, but input is not being considered.

It only works when running from command line, but that basically disallows for debugging.

Though the answer for this problem is here Debug Console window cannot accept Console.ReadLine() input during debugging

Egbert Nierop
  • 2,066
  • 1
  • 14
  • 16