Question How to control what the user enters and give an error if needed?


New member
Mar 12, 2014
Programming Experience
Hello, I am making a C# console application which calculates BTEC grades to UCAS points.

Only thing is, this course has 18 units, so I want the user to enter either 'p', 'm' or 'd' and maybe some other inputs if I choose to later.
So when the user enters something wrong like 'wefttryh239y' then I want the program to output an error message.
I could use a string and check it using if statements, but I feel that isn't very efficient, plus I would like to know just in case I want the user to enter only numbers.
Any idea on how I can do this?

Thanks. :kiwi-fruit:
You have no choice but to compare the user input to a list of valid inputs. Exactly how you do that is up to you. You might use a series of if...else if statements or a switch statement or you might put the valid inputs into an array or collection and then loop through that or call Contains on it.
Top Bottom