Solved
I have made a square root calculator
It works when someone enters an invalid Input to the square root it repeats
however, when it says to enter the decimal value and there is a error it goes through the try-catch and goes and repeats the method. I would like for when the user is asked to input a number of decimals and when invalid input is typed it will repeat that question
https://github.com/AbdulRaheemNazir/MiniApps/blob/main/Square%20Root%20help
here is the link to the code, any help would be appreicated it
GitHub
MiniApps/Square Root help at main · AbdulRaheemNazir/MiniApps
Contribute to AbdulRaheemNazir/MiniApps development by creating an account on GitHub.
12 Replies
$tryparse
The TryParse pattern is considered best practice of parsing data from a string:
- a TryParse method returns
true
or false
to inform you if it succeeded or not, so you can use it directly in a condition,
- since C# 7 you can declare a variable that will be used as an out
argument inline in an argument list,
- it forces you to check if the out
argument contains valid data afterwards,
Avoid: Convert.ToInt32 — it's a bad choice for parsing an int
. It exists only for backwards compatibility reasons and should be considered last resort. (Note: Convert does contain useful conversion methods: To/FromBase64String
, To/FromHexString
, ToString(X value, int toBase)
, ToX(string? value, int fromBase)
)
Avoid: int.Parse — you have to use a try
/catch
statement to handle invalid input, which is a less clean solution.
Use int.TryParse https://docs.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-5.0#System_Int32_TryParse_System_String_System_Int32__ Int32.TryParse Method (System)
Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
So the restart of the question is fine for int number
But for the int numberofdecimalplaces how woul I get it to repeat that question again
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Thank you for your help
--------------------------------------------
forget everything i said above issue is now fixed
-------------------------------------------------
GitHub
MiniApps/Repeat asking for decimal number at main · AbdulRaheemNazi...
Contribute to AbdulRaheemNazir/MiniApps development by creating an account on GitHub.
Everything works but I want it to repeat asking the user for a valid decimal number
i have added a while loop but i dont think i have implemented it right
as when i write a invalid decimal number it leads me to the start of the program
Anyone?
numberofdecimalplaces > 0 & numberofdecimalplaces < 7
You're using bitwise and &
You should use &&
i.e logical andGood spot, thank you but that I don't think would make a difference to the problem I'm facing
Right, it won't really affect the execution, I don't see any bug tbh, the program will keep asking for numberofdecimalplaces until it's provided a valid one
Oh it does work🤣I was looking at the old version😭😭