C
Join ServerC#
help
❔ Can not figure out a way to make my code repeat until you type a valid input
TIThisIan III12/27/2022
im having a hard time figuring out how to make my code repeat until you enter a number and if you enter a letter or some other character it will just repeat itself
im also kinda new to this so please dont butcher me
im also kinda new to this so please dont butcher me
namespace CSLearning
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter your type of equation(+, -, *, /): ");
string typeOfEquation = Console.ReadLine();
if (typeOfEquation.Contains('+'))
{
double num1 = 0;
double num2 = 0;
Console.Write("Enter your first adden: ");
string adden1 = Console.ReadLine();
Console.Write("Enter your second adden: ");
string adden2 = Console.ReadLine();
num1 = Convert.ToDouble(adden1);
num2 = Convert.ToDouble(adden2);
Console.WriteLine("The awsnwer to your equation is " + num1 + num2);
}
Console.ReadLine();
}
}
}
AAngius12/27/2022
Use a loop
AAngius12/27/2022
A
while
loop, most commonlyAAngius12/27/2022
And $tryparse to check if the entered value is actually a number. And if so, to get the parsed value out
MMODiX12/27/2022
The TryParse pattern is considered best practice of parsing data from a string:
- a TryParse method returns
- since C# 7 you can declare a variable that will be used as an
- it forces you to check if the
Avoid: Convert.ToInt32 — it's a bad choice for parsing an
Avoid: int.Parse — you have to use a
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__
- 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. return Convert.ToInt32(null); //returns 0. null should not be considered as 0 ever
return Convert.ToInt32("asdf"); //throws FormatException
(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. var number = int.Parse("abc"); //throws FormatException
var number = int.Parse(""); //throws FormatException
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__
if (int.TryParse(someInput, out var result))
{
Console.WriteLine($"Thanks for giving me the following number: {result}!");
}
else
{
Console.WriteLine("You didn't give me a valid number :c");
}
TIThisIan III12/27/2022
how do I add the while loop in the code
AAngius12/27/2022
Just... do
AAngius12/27/2022
Write it
TIThisIan III12/27/2022
huh?
AAngius12/27/2022
Adding a while loop is no different than adding any other piece of code
AAngius12/27/2022
Just stroke the keys on your keyboard to input proper symbols that make up valid syntax
WWindows10CE12/27/2022
there's a learning module on while loops if you need it
WWindows10CE12/27/2022
TIThisIan III12/27/2022
while (adden1 != double)
is the only thing that comes to my mind and it doesnt work
AAngius12/27/2022
I told you to use
TryParse()
since it returns a booleanAAngius12/27/2022
The bot even posted more explanation
AAccord12/28/2022
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.