+ 4

cs console ReadLine

when i use Convert.ToInt32(Console.ReadLine()); in order to be able to enter any other value in "sandbox" it shows error. who knows why?

27th May 2018, 3:33 PM
Shodmon Malakhov
7 Réponses
+ 9
Shura, your code with x=Convert.ToInt32(Console.ReadLine()); Console.WriteLine (Fact(x)); works fine. Which error did you get? The code: https://code.sololearn.com/c9bWDpLYyf3E/?ref=app P.S. Песочница - Playground😊
27th May 2018, 5:32 PM
NezhnyjVampir
+ 7
The exception "FormatException" can be thrown. Handle it with try... catch. Here is an example: int a; try { a = Convert.ToInt32(Console.ReadLine()); } catch (FormatException){ Console.ReadLine("Write a number!"); } It is thrown each time you don't write a number!
27th May 2018, 4:00 PM
Codespirit
Codespirit - avatar
+ 6
What is this "sandbox" you're talking about anyways? (Edit) Confirmed, "sandbox" = Code Playground ; ) I saw your comment on your factorial code, and inserted x=Convert.ToInt32(Console.ReadLine()); at line 24 with no problem, I think you need to explain some more. And FYI it is highly recommended to attach the code link when asking a code related question. Do you know how to link your code with the question?
27th May 2018, 3:58 PM
Ipang
+ 5
The OverflowException can be thrown if the result is greater than 2^32-1. My tip: Use Int64 instead! The StackOverflowException can be thrown if you enter "0".
27th May 2018, 5:49 PM
Codespirit
Codespirit - avatar
+ 5
thanks guys. solved
27th May 2018, 7:04 PM
Shodmon Malakhov
+ 5
Ipang that is playground, as already mentioned by NezhnyjVampir. Sorry for my "french"
27th May 2018, 7:19 PM
Shodmon Malakhov
+ 4
Shura I think you are saying you want the program to run even if a user enters something like '5.03' which is considered a decimal. If this is the case, you have to convert to decimal first and then convert the decimal to an int. Let me know I miss read your question. btw- you should provide a link to the code you need help with and it is always a good thing to post the exception you received. Help the community to help you.
27th May 2018, 5:48 PM
ODLNT
ODLNT - avatar