How do I ask the user for input in C#
Asked Answered
M

5

8

I am switching from Python to C# and I am having trouble with the ReadLine() function. If I want to ask a user for input Python I did it like this:

x = int(input("Type any number:  ")) 

In C# this becomes:

int x = Int32.Parse (Console.ReadLine()); 

But if I type this I get an error:

int x = Int32.Parse (Console.ReadLine("Type any number:  "));

How do I ask the user to type something in C#?

Mutter answered 21/2, 2017 at 12:1 Comment(3)
What is the error?Zama
You must first Console.WriteLine() and then Console.ReadLine()Nork
Console.Write("Type any number:"); or Console.WriteLine then you can do the read lineZimmermann
A
8

You should change this:

int x = Int32.Parse (Console.ReadLine("Type any number:  "));

to this:

Console.WriteLine("Type any number:  "); // or Console.Write("Type any number:  "); to enter number in the same line

int x = Int32.Parse(Console.ReadLine());

But if you enter some letter(or another symbol that cannot be parsed to int) you will get an Exception. To check if entered value is correct:

(Better option):

Console.WriteLine("Type any number:  ");

int x;

if (int.TryParse(Console.ReadLine(), out x))
{
    //correct input
}
else
{
    //wrong input
}

Starting from C# 7 you can use inline variable declaration (out variables):

Console.WriteLine("Type any number:  ");

if (int.TryParse(Console.ReadLine(), out var x)) // or out int x
{
    //correct input
}
else
{
    //wrong input
}
Asdic answered 21/2, 2017 at 12:2 Comment(2)
One imp thing... If you are not sure about the input and want to check it first (which is a good idea) try TryParse instead of Parse...Zama
Note that you can now declare the variable inline: int.TryParse(Console.ReadLine(), out int x). It will be accessible in the if.Gristmill
F
1
Console.WriteLine("Type any number");
string input = Console.ReadLine();
int x;
if (int.TryParse(input, out x))
{
    //do your stuff here
}
else
{
    Console.WriteLine("You didn't enter number");
}
Finkle answered 21/2, 2017 at 12:5 Comment(0)
K
1

To be more generic I would suggest you to make an additional object ( because you cannot extend static objects in C# ) to behave like you've specified.

public static class ConsoleEx
{
    public static T ReadLine<T>(string message)
    {
        Console.WriteLine(message);
        string input = Console.ReadLine();
        return (T)Convert.ChangeType(input, typeof(T));
    }
}

Of course you this code is not error free because it does not contains any constraints about the output type but still It will cast into some types without any problems.

For example. Using this code :

static void Main()
{
    int result = ConsoleEx.ReadLine<int>("Type any number: ");
    Console.WriteLine(result);
}

>>> Type any number: 
<<< 1337
>>> 1337 

Check this online

Kneeland answered 21/2, 2017 at 12:17 Comment(0)
L
0
Console.WriteLine("Type any number: ");
string str = Console.ReadLine();
Type a = Type.Parse(str);

where Type is Data Type you want to cast user input to. I suggest reading few books on C# fundaments before turning to forums.

Leatherworker answered 21/2, 2017 at 12:6 Comment(0)
A
-1

try this

Console.WriteLine("Type any number:  ");
int x = Int32.Parse (Console.ReadLine());
Asgard answered 21/2, 2017 at 12:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.