C
C#8mo ago
Mary Cooper

beginner help

No description
22 Replies
Mary Cooper
Mary CooperOP8mo 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
SpReeD8mo ago
One way is recursion, extract the method to an actual function and check the return value.
Mary Cooper
Mary CooperOP8mo 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
SG978mo ago
what is the valid testScore?
Mary Cooper
Mary CooperOP8mo ago
Just anything from 0-100
SG97
SG978mo ago
so loop until you get a valid testScore?
Mary Cooper
Mary CooperOP8mo 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 CooperOP8mo ago
No description
SG97
SG978mo 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 CooperOP8mo ago
So instead of using a string I could use a Boolean? Bool Validinput = false. And put !validInput in the while?
SG97
SG978mo ago
exactly, and the only way validInput becomes true, is when the TryParse is successful + the result is between 0-100
Mary Cooper
Mary CooperOP8mo ago
Yep it kind of worked
SG97
SG978mo ago
kind of?
Mary Cooper
Mary CooperOP8mo ago
No description
SG97
SG978mo 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 CooperOP8mo ago
So where would I put the Validinput?
SG97
SG978mo 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 CooperOP8mo ago
Ohh okay that’s making more sense
SG97
SG978mo ago
assuming here that 0 and 100 are both invalid
Mary Cooper
Mary CooperOP8mo ago
You can do 0 or 100 but that doesn’t matter
SG97
SG978mo ago
you can refactor the code
Mary Cooper
Mary CooperOP8mo ago
Yeah Okay I’ll give that one a shot Thankyou I appreciate the help
Want results from more Discord servers?
Add your server