C
C#3mo ago
Mary Cooper

✅ Beginner loop

I need to make it so if the user doesnt enter an integer it gives an error. ive done this but if they enter 'cat' the error only shows up for a a split second and then the program closes. do { try { Console.WriteLine(); Console.Write("Input test score: "); testscore = Convert.ToInt32(Console.ReadLine()); } catch { Console.WriteLine("Invalid Integer... Press Enter To Exit."); Console.ReadLine(); return; }

if (testscore == -1) { Console.Write("Well done, Pres ENTER To Exit...."); Console.ReadLine(); Console.WriteLine(); Environment.Exit(0); } else if (testscore < -1) { Console.WriteLine("Invalid Integer. Please Retry"); } else if (testscore > 100) { Console.WriteLine("Invalid. Please Enter Your Test Score."); }
else if (testscore >= 0 && testscore <= 49) { Console.WriteLine("You need to put in more work!");
} else if (testscore >= 50 && testscore <= 79) { Console.WriteLine("Could you do better? ");
} else if (testscore >= 80 && testscore <= 100) { Console.WriteLine("Well Done!");
}
} while (true);
} } }
43 Replies
Buddy
Buddy3mo ago
$paste
MODiX
MODiX3mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Buddy
Buddy3mo ago
Your error is possibly because of this $tryparse
MODiX
MODiX3mo ago
When you don't know if a string is actually a number when handling user input, use int.TryParse (or variants, e.g. double.TryParse)
if(int.TryParse("123", out int number))
{
var total = number + 1;
Console.WriteLine(total); // output: 124
}
if(int.TryParse("123", out int number))
{
var total = number + 1;
Console.WriteLine(total); // output: 124
}
TryParse returns a bool, where true indicates successful parsing. Remarks: - Avoid int.Parse if you do not know if the value parsed is definitely a number. - Avoid Convert.ToInt32 entirely, this is an older method and Parse should be preferred where you know the string can be parsed. Read more here
Buddy
Buddy3mo ago
Convert.ToInt32, etc just uses Parse. Issue with parse is that if the format is invalid, an exception will be thrown and crash your program if unhandled. TryParse will give a boolean whether it succeeded or not
Mary Cooper
Mary Cooper3mo ago
alright so you i just remove the try catch?
Buddy
Buddy3mo ago
if you use TryParse, yes.
Mary Cooper
Mary Cooper3mo ago
Is there any way at all to do it with try catch?
SinFluxx
SinFluxx3mo ago
You can still do it with try/catch, just that your catch should be at the end of your loop and it shouldn't have a return in it
Pobiega
Pobiega3mo ago
yes, try/catch with int.Parse inside a loop will work fine
Mary Cooper
Mary Cooper3mo ago
so i put catch down the bottom but on top of the while?
Pobiega
Pobiega3mo ago
wat The code you have right now is really hard to read, too much repetition and nested statements think of what must be repeated and what must not
Mary Cooper
Mary Cooper3mo ago
any way u can make it easier for you i*
Pobiega
Pobiega3mo ago
well, what I'm trying to say is that this would be easier for you too, if you could separate what parts go where
Mary Cooper
Mary Cooper3mo ago
ahh
Pobiega
Pobiega3mo ago
What distinct "steps" could we separate your code into?
Mary Cooper
Mary Cooper3mo ago
okay so basically the task is just depending on the number it displays a certain message but i need to include error handling for incorrect data types
Pobiega
Pobiega3mo ago
Sure so step 1 would be reading an input and making sure its a valid number
Mary Cooper
Mary Cooper3mo ago
yes
Pobiega
Pobiega3mo ago
what would be step 2?
Mary Cooper
Mary Cooper3mo ago
the message that it displays depending on the number entered
Pobiega
Pobiega3mo ago
okay, maybe rephrase that to "print a message based on the score" but yeah, it works so, what parts needs to repeat?
Mary Cooper
Mary Cooper3mo ago
the number that the user is entering it needs to loop until -1 is typed in, then it exits weird task but thats what ive been assigned
Pobiega
Pobiega3mo ago
Okay, so the program as a whole repeats until -1
Mary Cooper
Mary Cooper3mo ago
yes
Pobiega
Pobiega3mo ago
I see. Well, that explains your current code. With a few tweaks, this structure works. But only because its a simple program - if it were to grow this would quickly become unmanagable
Mary Cooper
Mary Cooper3mo ago
yes this is all i need to do once i do the error handling its all done
Pobiega
Pobiega3mo ago
You have only a single loop, the one for your entire program. That means that if the user enters an invalid number, your entire program starts over how I would write this would be that the "get and validate a number" thing would be its own part of the code, and it wouldnt be able to leave that loop until the user entered a valid number
Mary Cooper
Mary Cooper3mo ago
yes
Pobiega
Pobiega3mo ago
But you can keep it as is, you just need to fix some small issues - your usage of return specifically entering an invalid number probably shouldnt stop the program why not just say "invalid number, try again" and.. go again?
Mary Cooper
Mary Cooper3mo ago
would that be easier?
Pobiega
Pobiega3mo ago
? I dont understand the question.
ive done this but if they enter 'cat' the error only shows up for a a split second and then the program closes.
because tahts what your code does.
catch
{
Console.WriteLine("Invalid Integer... Press Enter To Exit.");
Console.ReadLine();
return;
}
catch
{
Console.WriteLine("Invalid Integer... Press Enter To Exit.");
Console.ReadLine();
return;
}
Mary Cooper
Mary Cooper3mo ago
yes so, if i enter a number its fine. but i need it so if user does not enter an integer it displays Invalid Integer... Press Enter To Exit.
Pobiega
Pobiega3mo ago
that return; there means "stop this method"
Mary Cooper
Mary Cooper3mo ago
instead of doing that it closes instantly even without the return the program shuts down
Pobiega
Pobiega3mo ago
.. respectfully, no it doesnt
Mary Cooper
Mary Cooper3mo ago
as soon as a type a letter and hit enter, it shuts down could my visual studio be bugging
Pobiega
Pobiega3mo ago
have you perhaps set it to do the really stupid thing of running the last compilable version of your code on errors?
Mary Cooper
Mary Cooper3mo ago
uhhh how would i check that? id hope not okay, so i just cleaned and rebuilt solution i did not know that was a thing
Pobiega
Pobiega3mo ago
No description
Mary Cooper
Mary Cooper3mo ago
yep
Pobiega
Pobiega3mo ago
make sure this is set to "Do not launch"
Mary Cooper
Mary Cooper3mo ago
i have spent so long trying to fix this and all i had to do was that sorry about that thankyou