C
C#9mo ago
Mary Cooper

✅ Beginner help

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Exercise_2_Task_1 { internal class Program { static void Main(string[] args) { int testscore = 0; do { Console.WriteLine(); Console.Write("Input test score: "); testscore = Convert.ToInt32(Console.ReadLine());
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 Integer. Please Retry"); }
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);
} } } How can i make it so if they enter like a word 'dog' it says invalid. And Re loops again
22 Replies
Mary Cooper
Mary CooperOP9mo ago
i mean, if the user enters anything but an interger, it just says invalid, Please Enter a valid integer and then displays the Input test score again
SinFluxx
SinFluxx9mo ago
$tryparse
MODiX
MODiX9mo 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
Mary Cooper
Mary CooperOP9mo ago
is there anyway to do it with the else, else if or if loops? i mean without tryparse
Pobiega
Pobiega9mo ago
When pasting $code, its really helpful if you do it right so we get syntax highlighting and a monospace font.
MODiX
MODiX9mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Mary Cooper
Mary CooperOP9mo ago
i havent learnt that yet okay
maxmahem
maxmahem9mo ago
Well you can parse it manually but... TryParse will be way easier.
Pobiega
Pobiega9mo ago
TryParse is 100% the way to go here. Your alternatives are manual parsing (much harder) or try/catch (more complicated)
TheRanger
TheRanger9mo ago
well you could iterate through each character in the string and check if its a digital number or not
maxmahem
maxmahem9mo ago
It is interesting to learn the pattern try parses uses though.
Mary Cooper
Mary CooperOP9mo ago
i know but if they see ive used something we havent covered in class that wont go well for me we have covered try catch
Pobiega
Pobiega9mo ago
then use try catch
TheRanger
TheRanger9mo ago
^
Pobiega
Pobiega9mo ago
but be aware that its the bad way to do this, in the future
Mary Cooper
Mary CooperOP9mo ago
yeah im noty sure why he hasnt taught us tryparse
hoggy077
hoggy0779mo ago
Since you stipulated it should loop, you can also use continue; in future to skip the current loop & move onto the next
Mary Cooper
Mary CooperOP9mo ago
okay thankyou guys
maxmahem
maxmahem9mo ago
int result = 0;
foreach (char c in input)
if (IsDigit(c))
result = result * 10 + (c - '0');
int result = 0;
foreach (char c in input)
if (IsDigit(c))
result = result * 10 + (c - '0');
this is like the 'standard' int parsing pattern.
Mary Cooper
Mary CooperOP9mo ago
okay i see thanks
TheRanger
TheRanger9mo ago
if they taught them int.Parse i dont see why we should use this or Convert.ToInt32
maxmahem
maxmahem9mo ago
🤷 I don't either, but they asked for a method using loops. this is a method using loops.
Want results from more Discord servers?
Add your server