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

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.

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?

is there somebody that can fix this if statement [duplicate]

This question already has answers here:
How to stop C# console applications from closing automatically? [duplicate]
(9 answers)
Closed 2 years ago.
static void Main(string[] args)
{
string name01;
string name02;
Console.WriteLine("Welcome to Choice RPG! <press enter to start your adventure>");
Console.ReadKey();
Console.WriteLine("Enter your Adventurers name! <press enter to continue>");
name01 = Convert.ToString(Console.ReadLine());
Console.WriteLine(name01 + "! come on, you can think of a better name than that can't you? <press enter to continue>");
Console.ReadKey();
Console.WriteLine("Enter your Adventurers name! <press enter to continue>");
name02 = Convert.ToString(Console.ReadLine());
if (name02 == name01)
{
Console.WriteLine("Wow, i guess you really like that name huh, fine.");
}
else
{
Console.WriteLine("Now thats more like it!");
}
}
This is my first project with programming in general. I am making an RPG game in which you choose your adventure and this is for the user's ingame name.
Everything works up until the if statement (after that the cmd just closes). If anyone has any ideas to fix it please tell me.
Code looks OK to me, try adding:
Console.WriteLine("Press <ENTER> to exit");
Console.Readline();
at the end to see if the message you want to show, actually shows before the application exits and closes. My guess is you are running in Visual Studio, and the message prints, but then the execution window closes so fast you don't see it.

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();

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.

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