Only part of a program executing - c#

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.

Related

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

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?

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.

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

C# Console Application Crashing

On Visual Studio, the C# Console application keeps on terminating when I try to run a program. I tested this by just writing Hello World, shown here:
(screenshot of what I wrote)
When I run it, the command prompt window appears for a second, then immediately closes. Is there a way to fix this?
When a console application runs it executes the code and exits, console applications do not have a message loop to keep them alive, it is the same as creating a batch file and double clicking it, the command window will open and then close when execution finishes. If you want then console window to hang around afterwards then you need to use Console.ReadLine(); which will wait for user input before closing (i.e. pressing Enter).
You code (which should be in your question) simply outputs some text to the console and then exits.
Suggested reading Creating a Console Application
class Program
{
static void Main (string[] args)
{
Console.WriteLine("Hello World");
Console.ReadLine();
}
}
The application is not crashing, it runs and then exits. If you want read the output after it's done you can use Console.ReadLine();
using System;
namespace Hello_World
{
class Program
{
static void Main (string[] args)
{
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}
The issue is not crashing really but that all the instructions are executed then the program exits. If you desire to pause, you could insert the statement:
Console.ReadKey();
which will wait for you to type a key to exit.

C# Visual studio Issues with install and after

Recently i have decided to learn c# using the IDE VB platform. I downloaded the open sourced software on the Microsoft website however i have had constant problems since none of which i received with any Java alternative.
I have downloaded and uninstalled it twice, the first time i try to run any form of class even a blank program. I will get a warning stating a Trojan Horse is present (Luckily my AVG managed to get it).
Then when ever i try to run the file, i will get the error code CS2012, and nothing happens.
I am wondering does anyone have any ideas on how to fix it or potentially know of any other free software for c#.
I have included the code which I am using below.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("basic input output");
GetUserData();
Console.ReadLine();
}
static void GetUserData()
{
Console.Write("please enter your name: ");
string userName = Console.ReadLine();
Console.Write("please enter your age: ");
string userAge = Console.ReadLine();
//changes echo colour
ConsoleColor prevColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.red;
//echo to console
Console.WriteLine("hello {0}! Your are {1} years old.", userName, userAge);
//Restore previous color
Console.ForegroundColor = prevColor;
}
}
Thanks Joe
This is actually a common issue:
I guess your virus scanner detects YOUR program as suspicious and prevents the execution (or kills it right away). I have the same problem with Avast AntiVirus (although it does give me a very understandable error message that it is MY program, which it is not sure about).
Solution: Turn off the virus check in the folder you are programming at (i.e. the folder with the solution of the project). I don't know AVG personally, but you should be able to set some sort of exception which are not included in the scan process.
Note: I do not take any responsibility if it is really a virus of some sort.

Categories

Resources