C
C#15mo ago
ari ishtar

❔ i need some help getting out of loop

do
{

try
{
Console.WriteLine();
Console.WriteLine("please enter one of the names shown above. (correct spelling)");
gasName = Console.ReadLine();

}
catch (System.FormatException ex)
{
Console.WriteLine("not a gas == " + ex.Message);
good = false;
}
catch (System.OverflowException ex)
{
Console.WriteLine("not the right name == " + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("error data == " + ex.Data + " == " + ex.Message);
}
finally
{
Console.WriteLine("will you like to try again? ");
if (good)
{
doAgain = "no";
}
else
{
doAgain = "yes";
}
}

} while (doAgain == "no");
do
{

try
{
Console.WriteLine();
Console.WriteLine("please enter one of the names shown above. (correct spelling)");
gasName = Console.ReadLine();

}
catch (System.FormatException ex)
{
Console.WriteLine("not a gas == " + ex.Message);
good = false;
}
catch (System.OverflowException ex)
{
Console.WriteLine("not the right name == " + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("error data == " + ex.Data + " == " + ex.Message);
}
finally
{
Console.WriteLine("will you like to try again? ");
if (good)
{
doAgain = "no";
}
else
{
doAgain = "yes";
}
}

} while (doAgain == "no");
im in a infinite loop and not sure how to get out. tldr im very new as im still trying to learn
10 Replies
JakenVeina
JakenVeina15mo ago
always start with properly defining your problem what is the loop behavior you WANT to achieve?
ari ishtar
ari ishtarOP15mo ago
im stuck in a loop and i cant get out of it, and i need a way to check to see if a user input exists in the array i have i dont know how more define my problem then that honestly
Kouhai
Kouhai15mo ago
Can you check your condition in the while statement?
JakenVeina
JakenVeina15mo ago
then that's where younneed to start what do you want this loop to do? what is it supposed to do?
ari ishtar
ari ishtarOP15mo ago
so i aks the user what gas they want they enter it, then the different exceptions to make sure they write the name correctly if not they are force back to try again. once they enter correct they move on
phaseshift
phaseshift15mo ago
Look at your while condition
ari ishtar
ari ishtarOP15mo ago
thank you
Buddy
Buddy15mo ago
Wait how can you get formatexception or even overflowexception if you only read input from console as a string? Hell any exception
Jimmacle
Jimmacle15mo ago
yeah the exception handling doesn't make any sense, they will never be thrown let alone thrown for what you seem to expect in that code don't use exceptions for flow control regardless they're for things you don't expect to happen, but you clearly expect that the user might input an invalid/wrong gas name
Accord
Accord15mo ago
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.
Want results from more Discord servers?
Add your server