Simple C# Code Prints Random Garbage - c#

using System;
class MainClass {
public static void Main() {
string name;
Console.Write("Hello World: ");
name = Console.ReadLine();
if (name != null && name == "Test") {
Console.WriteLine("Test input.");
} else {
Console.WriteLine("Test not input.");
}
}
}
Hi, above is my simple C# code. I'm new to C#, although I am not new to programming as a whole. When I try this code (which I know has redundancy in the if statement, that's not the point) it prints garbage.
The code works normally, it just starts off like this:
Hello World: [6n[H[J[6n
I have no clue why it's adding [6n[H[J[6n. I tried looking it up, but I'm really at a loss. This code was working a few days ago, so maybe it's a compiler issue.
If anyone could help out, that would be much appreciated.

those are ansi console control codes. in ansi, [6n is "query cursor position", [h is "home" and [j is "erase down" http://www.termsys.demon.co.uk/vtansi.htm
To me, it looks like you were possibly pressing keys like home/delete/etc, and whatever console app you are running in is displaying them instead of interpreting them.
what kind of console are you running it in? were you pressing other keys while it is waiting?

Related

Cant use Console.Readline() in simple Console Application? [duplicate]

This question already has answers here:
How to stop C# console applications from closing automatically? [duplicate]
(9 answers)
Closed 3 years ago.
Im running .NET Core 2.2 and just want to create a simple Console Application that reads one value and prints it out. Yes i know there is millions of examples of this but my problem is that none of them works for some reason. When i press Enter the application stops.
Here is the simple code that just writes the first line but then do nothing:
static void Main(string[] args)
{
Console.WriteLine("I want to write a value here");
var theValue = Console.ReadLine();
Console.WriteLine(theValue);
}
Is there any configuration i have to do in Visual Studio 2017 to make the Console read my value i write? Or anything else that makes the Console close and program to end when i press enter?
The application doesn't stop, It simply ends since it prints out the result of your line and moves to next line since there is no more code to run it simply closes the application
Add Console.ReadLine(); on the end so that it doesn't close after first enter
static void Main(string[] args)
{
Console.WriteLine("I want to write a value here");
var theValue = Console.ReadLine();
Console.WriteLine(theValue);
Console.ReadLine();
}
you need to make the app wait and not exit using an additional Console.Read. Try like:
var theValue = Console.ReadLine();
Console.WriteLine(theValue);
Console.Read();

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.

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.

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.

Trouble getting the last bit when reading output from process

I am building a small program to help me when I use a server console for a game (hence I do not have the source for the console program).
I have the console starting up in the background and it redirects the output and I then write it in cute little textbox.
Problem is that It doesn't seem to give me the last line every time it makes an output.
This is some code from my server.cs:
public StreamWriter inputWriter;
public event EventHandler outputWritten;
string outtext = outputReader.ReadLine();
while (!process.HasExited)
{
// check for output from server
if (outtext != null)
{
if (outputWritten != null)
{
outputWritten(outtext, new EventArgs());
}
}
outtext = outputReader.ReadLine();
}
This is pretty much what the output would look like:
1) If it would run outside my program:
Welcome to the game server!
1 OptionA
2 OptionB
3 OptionC
Choose option:_ <-- this is where it should end waiting for input
2) If inside my program:
It would look like the above but wouldn't show the last line in stead it would end right at the empty line above the "Choose option:"
And when I give it an input it messes it up a bit and I get an output looking like "Choose option: Welcome to the game server!" where it should have written: "Choose option: 1" and on the next line given my a small question on how to proceed.

Categories

Resources