C# Visual studio Issues with install and after - c#

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.

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?

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.

Simple C# Code Prints Random Garbage

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?

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