Reputation: 21
how to take input of int or double type in C# in console??? i had taken input in C++ and C .But in C# ,i am not able to give user input at the run time. So tell how to take input at the run time in C#.
Upvotes: 0
Views: 9932
Reputation: 107
Try this -
double a; int b;
a = Convert.ToDouble(Console.ReadLine()); // Value in double
b = Convert.ToInt32(Console.ReadLine()); // Value in Int
Upvotes: 3
Reputation: 1323
Try using
Console.ReadLine();
ex:
Console.Writeline("1. Do Something");
string input = Console.ReadLine();
Upvotes: 0
Reputation: 223257
Use Console.ReadLine
to read input as string and then convert them to your required type, using int.Parse
or int.TryParse
, or double.Parse
, or double.TryParse
like:
string input = Console.ReadLine();
int temp;
if (int.TryParse(input, out temp))
{
//valid int input
}
else
{
//invalid int input
}
Console.WriteLine(temp); //input number
Its better if you use TryParse
family of methods for parsing, since they will not raise an exception in case of failed parsing.
You may also see: How to: Convert a String to a Number (C# Programming Guide)
Upvotes: 1
Reputation: 9646
Use Console.ReadLine() that lets you to put an input and you can assign it to the input you wish, if int then parse it.
Upvotes: 0
Reputation: 887453
You need to read a line of textual input using Console.ReadLine()
, then parse it as a number using int.Parse()
or double.TryParse()
or other variants.
Upvotes: 1