C# - checking if input is higher than 0. I need an idea :\ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

C# - checking if input is higher than 0. I need an idea :\

Hi! I made a code that using the user's input to calculate a sqrt without Math.sqrt. The thing is, that the code has to validate if the user's input is not a text and not a negative number. I can manage to check only one or another. How to check both conditions at the same time? Thank you in advance for any help! Here is the code: https://pastebin.com/BXXm5AnX

20th Oct 2019, 1:06 AM
DIY Mods
DIY Mods - avatar
4 Answers
+ 7
Basically, check through the input to see if all are numbers. Then convert it to a numeric type and see if the value is less than 0
20th Oct 2019, 1:28 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 2
DIY Mods Sounds like regex does exactly what you want in one line. https://code.sololearn.com/ce93AeTth5ae/?ref=app
20th Oct 2019, 2:16 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
@Prometheus [EXAMS]🇸🇬 Sounds great but doesn't work for some reason
20th Oct 2019, 2:42 AM
DIY Mods
DIY Mods - avatar
0
Why are you calling double.TryParse and Convert.ToDouble? You already have the value as a double in number. I would just do if (double.TryParse(textBox1.Text, out number) && number >= 0)
20th Oct 2019, 5:09 AM
Jesse Bayliss
Jesse Bayliss - avatar