how to I take int user input
what am i doing wrong it is saying that i cant take in the int because it is a string. What is the easiest way to to take int user input
8 Replies
$tryparse
When you don't know if a string is actually a number when handling user input, use
int.TryParse
(or variants, e.g. double.TryParse
)
TryParse
returns a bool
, where true
indicates successful parsing.
Remarks:
- Avoid int.Parse
if you do not know if the value parsed is definitely a number.
- Avoid Convert.ToInt32
entirely, this is an older method and Parse
should be preferred where you know the string can be parsed.
Read more hereVariables should be camel case, instead of upper camel case:
To properly handle user input, you'll probably have to use loops.
When the user enters anything but a number, the parsing will fail. You'll want to display some error message to remind the user that you want a numeric value and ask for input again.
Maybe
NumRed = Convert.ToInt32(Console.ReadLine());
Try this
So this error says, that you must convert string to int
So you must convert it
Like this
Understand?
Please no. Literally says in the previous message not to use Convert
It's a legacy method, do not use it. Period.
If you wish to convert your number unsafely without checking whether the string is truly a number, use
Just read this. If you have additional questions, go ahead
Thank you i will try this today
Thank you