C
C#3mo ago
Mary Cooper

beginner help

No description
22 Replies
Mary Cooper
Mary Cooper3mo ago
I’m trying to figure out how to get it to re ask to enter the student test score instead it just says the error message but carries on with the rest of the program
SpReeD
SpReeD3mo ago
One way is recursion, extract the method to an actual function and check the return value.
Mary Cooper
Mary Cooper3mo ago
Okay i will have to look that up Still very new to all of this Is there any simpler ways? Just changing the way it’s looped?
SG97
SG973mo ago
what is the valid testScore?
Mary Cooper
Mary Cooper3mo ago
Just anything from 0-100
SG97
SG973mo ago
so loop until you get a valid testScore?
Mary Cooper
Mary Cooper3mo ago
If they enter anything under 0 or 100 it re asks them to enter test score. But if they enter letters or whatever it just carries on with the rest of the code but still displays the error Yes
Mary Cooper
Mary Cooper3mo ago
No description
SG97
SG973mo ago
you may want to change the while condition to check when a) it gets succesfully parsed as a double b) it's between 0-100. You could use a boolean for this
Mary Cooper
Mary Cooper3mo ago
So instead of using a string I could use a Boolean? Bool Validinput = false. And put !validInput in the while?
SG97
SG973mo ago
exactly, and the only way validInput becomes true, is when the TryParse is successful + the result is between 0-100
Mary Cooper
Mary Cooper3mo ago
Yep it kind of worked
SG97
SG973mo ago
kind of?
Mary Cooper
Mary Cooper3mo ago
No description
SG97
SG973mo ago
double.TryParse returns a boolean to indicate whether it was a successful so you take that result + the check for 0-100 and combine them to determine if it was a valid input
Mary Cooper
Mary Cooper3mo ago
So where would I put the Validinput?
SG97
SG973mo ago
var validInput = false;
validInput = (conversion was successful + range was between 0-100)
var validInput = false;
validInput = (conversion was successful + range was between 0-100)
pseudo code you can take 2 booleans, one for conversion and one for range, and check if both are true -> assign validInput = true or
var success = double.TryParse(input out var result);
validInput = success && result > 0 && result < 100;
var success = double.TryParse(input out var result);
validInput = success && result > 0 && result < 100;
Mary Cooper
Mary Cooper3mo ago
Ohh okay that’s making more sense
SG97
SG973mo ago
assuming here that 0 and 100 are both invalid
Mary Cooper
Mary Cooper3mo ago
You can do 0 or 100 but that doesn’t matter
SG97
SG973mo ago
you can refactor the code
Mary Cooper
Mary Cooper3mo ago
Yeah Okay I’ll give that one a shot Thankyou I appreciate the help
Want results from more Discord servers?
Add your server
More Posts