1

I am using VSCode to fiddle with C# and I have created a function that is supposed to print out a message (which it does) and then take an input and end the function (which it doesn't). I suspect that the issue is somewhere in Console.ReadLine(), and every guide I find shows Console.ReadLine() being used in exactly the same way, but nevertheless when I push enter while the program is running and after typing a valid String, the Console.ReadLine just repeats and I end up with a new line underneath the old one.

private static String Ask(String message){
    String messi = "";
    String ans = "";
    messi = message;
    Console.WriteLine(messi);
    Console.WriteLine("Arrived"); //This checks to make sure that we do reach ReadLine()
    //ans = Console.ReadLine();
    String s = Console.ReadLine();
    Console.WriteLine("ArrivedBefore");
    return ans;
}
  • Try replacing `return ans;` with `return s;` btw can you share the code which calls `Ask` method? – Chetan Apr 28 '22 at 02:58
  • Your code in the question works fine. Have you tested it in isolation? So the problem is somewhere else in your code - and you haven't shown us that code. You need to give us a [mcve]. – Enigmativity Apr 28 '22 at 02:58
  • @Chetan - How would that fix the OP's issue? – Enigmativity Apr 28 '22 at 02:59
  • `ans` is set to blank string. and value assigned to `s` is completely ignored in the code. So if OP is expecting the value of `s` to be displayed in the caller it is not happening because of return of incorrect value. @Enigmativity – Chetan Apr 28 '22 at 03:01
  • @Chetan - Which is a guess based on unseen code. I'd rather see the actual code. – Enigmativity Apr 28 '22 at 03:06
  • @Enigmativity - Yes it is guess... that's why it is not posted as an answer but as a comment starting with `try...`. – Chetan Apr 28 '22 at 03:18
  • *the Console.ReadLine just repeats* - what does it mean? You have one call to C.RL in your entire program; how can it repeat anything? – Caius Jard Apr 28 '22 at 07:01
  • 1
    Your entire code can be reduced to `private static String Ask(String message){ Console.WriteLine(message); return Console.ReadLine(); }` - dispense with all the fluffing around with unnecessary variables; it's a recipe for confusing yourself. Make sure you don't call Ask with a message of `""` - it's a recipe for confusing the user – Caius Jard Apr 28 '22 at 07:04
  • How is the method `Ask` called ? It is the method calling `Ask` that is the problem. – dennis_ler Apr 28 '22 at 07:33
  • 1
    Please provide enough code so others can better understand or reproduce the problem. – Community Apr 28 '22 at 07:35
  • Does this answer your question? [Debug Console window cannot accept Console.ReadLine() input during debugging](https://stackoverflow.com/questions/41195432/debug-console-window-cannot-accept-console-readline-input-during-debugging) – Luuk Mar 19 '23 at 10:50

2 Answers2

0

Is the issue here that you are running the code via the "Debug Console" tab rather than from the "Terminal" tab.

I have had the same issue in Visual Studio Code

  • running my code in Visual Studio Code via the run command while the Debug Console is selected then "the Console.ReadLine just repeats and I end up with a new line underneath the old one."
  • running my code from a Command Window (having navigated to my project folder containing my csproj file) and executing a "dotnet run" command the code works as expected
  • running my code in Visual Studio Code via the run command while the Terminal tab is selected and my code runs as expected
0

Go to terminal where your cs file is located and run (In my case it is called Variable.cs):

dotnet run Variable.cs

and it worked for me.