Visual Studio debugger closes upon receiving input from Console.ReadLine() - c#

I have the following simple piece of code:
namespace Ex04
{
internal class Program
{
static void Main(string[] args)
{
string name;
Console.WriteLine("Welcome to the index-character generator.);
Console.WriteLine("Please write your name:");
name = Console.ReadLine();
Console.WriteLine("The second letter of your name is \"{0}\", the fourth letter is \"{1}\", while the sixth letter is \"{2}\".", name[1], name[3], name[5]);
}
}
}
When entering the debugger from Visual Studio the command prompt will open asking for an input from the user. Although upon entering any input the command prompt simply continues to execute the statements and the command prompt will close without showing the execution stepwise as expected. The project is correctly created as a "Console App".
Could anybody help out in identifying the issue here? How do i 'run' the program in debugging mode from start till finish? Is the marked statement from where entering the debugger (F10) is done important?

Related

How to stop command window from closing after hitting enter

I am a complete beginner so I really have no idea what I'm doing.
namespace first_program
{
class first_program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.ReadLine();
Console.WriteLine("Hello World!");
}
}
}
I am using VS Code and whenever I run this code the command prompt opens up, but whenever I press enter after typing into it the window closes.
Is there something wrong with my code? Or is it a bug with VS Studio?
I have copied all of this code from a Youtube tutorial by Brackeys titled "How to Program in C#"
It also my be important for me to note that a message saying "The program '[10260] first program.dll' has exited with code 0 (0x0)." pops up in the debug terminal.
Place Console.ReadKey() after the writeLine stuff, and it will wait for a keyboard interaction to close.

Only part of a program executing

I'm following this tutorial https://mva.microsoft.com/en-us/training-courses/c-fundamentals-for-absolute-beginners-16169?l=83b9cRQIC_9706218949
and I can't get the program working although I have copied the exact code as in the tutorial.
I have Ubuntu 16.04 so I'm using Visual Studio Code. And I have .NET SDK version 2.1.403.
Here's the code for my program:
using System;
namespace Decision
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Miina's Big Giveaway");
Console.Write("Choose a door: 1, 2 or 3 ");
string userValue = Console.ReadLine();
if (userValue == "1")
{
string message = "You won a new car!";
Console.WriteLine(message);
}
Console.ReadLine();
}
}
}
The problem is that the program isn't writing the line "Choose a door...". Only when I stop the execution of the program, the line "Choose a door ..." appears in the debug terminal.
And if I try typing "1" while the program is still running nothing happens although it should go through the commands in the if statement. I can't figure out where's the problem.
Update on the debugging
When I'm debugging a light bulb appears next to the Console.Write -line. I'm not sure what that means.
Picture of the debugging result
Update
The program is working correctly when I run it through the terminal. So I guess I have to use the terminal with the Visual Studio Code. But it would be nice to use debugger, so if anyone knows how I could get it to work, let me know.
I suspect that Console.Write isn't being flushed (stdout on Linux is unbuffered, and is only flushed on a line ending).
Try Console.Out.Flush() as a work-around. It's not pretty, though.

Failing to read input from .net-core console application in vscode

I've been trying to get dotnet new console example project (for vscode) to work in Ubuntu 17.10.
I can get the default program to run:
using System;
namespace dotnet_console
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello world!");
}
}
}
But when i change it to read input as well, it gets really wonky...
using System;
namespace dotnet_console
{
class Program
{
static void Main(string[] args)
{
Console.Write("Name: "); // 1
var name = Console.ReadLine(); // 2
Console.WriteLine("Hello {0}!", name); // 3
}
}
}
The program builds, but it won't print Name:. However if i put breakpoints on line 1, 2 & 3, i can see that the program runs through ALL of them, but nothing prints. That is until i stop the debugging. Then it prints
Name:
The program '[16322] dotnet-console.dll' has exited with code 0 (0x0).
What is happening here? I'm guessing its a vscode thing, because it works as expected when ran from the terminal using dotnet run.
The Documentation states the following:
By default, processes are launched with their console output
(stdout/stderr) going to the VS Code Debugger Console. This is useful
for executables that take their input from the network, files, etc.
But this does NOT work for applications that want to read from the
console (ex: Console.ReadLine). For these applications, use a setting
such as the following
I found a solution for the problem here.
And the following Quote from the linked Documentation also states that changing the console property from the launch.json to either "externalTerminal" or "integratedTerminal "is going to help.
When this is set to externalTerminal the target process will run in a
separate terminal.
When this is set to integratedTerminal the target process will run
inside VS Code's integrated terminal. Click the 'Terminal' tab in the
tab group beneath the editor to interact with your application.
Correct - 'internalConsole' is not meant for programs that want to take console input. Here is the official documentation: https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window

Why does the ReadLine () method NOT pause for user input?

I began to study C#, but I ran into a problem. The ReadLine() method does not pause for user input. I just started C#, and it works on other people's programs, so I have no idea why. I am using Xamarin on a Mac.
Here is an example code that does not work:
using System;
namespace LearningC
{
public class LearningSharp
{
public static void Main (String[] args)
{
Console.WriteLine (Console.ReadLine ());
}
}
}
I found out the solution. Right click on your project and select Options. Then, select General under the Run tab and check "Pause console output" ("Run on external console" must be checked for this to work.)
Maybe your Project is set to be a Windows application. Right click on your Project name, click properties and change output type to Console Application.
I have the same problem of a C# project not stopping with Console.ReadLine().
Here is my code:
char myChar1;
string userName;
string userLastname;
Console.Write($"Insert a char: ");
myChar1 = (char)Console.Read();
Console.WriteLine($"Press ENTER to continue");
Console.ReadLine();
Console.Clear();
Console.Write($"Insert name: ");
userName = Console.ReadLine();
Console.Write($"Insert lastname: ");
userLastname = Console.ReadLine();
// More stuff here
I found that when I use the method Console.Read() the next method Console.ReadLine() does not stop at all.
So after inserting a char in the variable myChar1 the program goes forward and stops only after the method Console.Write($"Insert name: ");
The workaround is to place two Console.ReadLine() methods before the Console.Clear() one but I can not understand why this goes like that.

Console window keeps closing, even after I type in other statements

I am new to programming and as seems to be traditional I tried to create a "hello world" program in C#; however, as soon as I run the program it closes.
This is my code inside:
main()
console.writeline("hello world");
console.writeline("enter name");
console.writeline("where is the frikin console");
It's really annoying and I know it might be something simple for the additional users but how do I keep the window open.
Use Console.ReadLine(); or Console.ReadKey(); at the end of your program to wait for the return key or for any key.
You can build your program and run the exe from the command line, that will allow you to see the output.
If you want the program to remain running then adding the Read() statement is the traditional approach, as others have already said.
If you just want to see it in debugging and do not want or need the read statement then place a breakpoint at the end of the program during a debug session.
It's really quite simple.
After this line of code:
Console.WriteLine("where is the frikin console");
You need to add this:
Console.ReadLine();
That should work.
The reason the console closes is because you told it to write some stuff to the screen, after it has finished writing what you told it to write it simply closes itself all in the fraction of a second. if you add Console.ReadLine, the console will wait for you to input something before closing, like pressing a key on the keyboard.
Try adding Console.Read(). You need to pause execution somehow.
Console.WriteLine("hello world");
Console.WriteLine("enter name");
Console.WriteLine("where is the frikin console");
Console.ReadLine();
Console.ReadLine(); will close the console after you've hit (for example) enter.
Console.ReadKey(); will close the console after the next key-hit
You can read the console-contents with these methods,too
Console.WriteLine("hello world");
Console.WriteLine("enter name");
string name = Console.ReadLine();
Console.WriteLine("Your name is: " + name);
Console.ReadLine();
add below line at the end
Console.ReadLine();
You could use CTRL + F5 which will opens the command line and after execution of your code, it shows Press any key to continue.... This will be handy for you than adding few lines of code additionally.
Use
Console.ReadLine();
in the end of your code. You are having this problem because the program just write the message then it ends, that's why you can't see anything. By adding that line, you keep the program waiting something to be typed and you can read the message. After this, type something to end the program.
The window automatically closes after your program, you need to let it take some input, for example:
Add
Console.ReadLine();
Which takes a line of input (till "\n"). And your program will wait until somebody hit the return key (they can type anything and the program won't close: until you hit the return key. You can type in "hello world back what's up are you ok?" and nothing will happen.)
or
Console.ReadKey();
Which will take a character of input. This will make your program wait for the user to press any key and then closes.

Categories

Resources