Removed: Rule 4.
It means you’ve provided non-integer input from your command line.
The console input is a string. That means it’s as likely to be “apple” as it is to be “14”. Can you describe a way to convert “apple” to an integer? Neither can .NET, thus Convert.ToInt32()
throws an exception. The input string (what came from the console) is not in the correct format (it’s not a number).
This isn’t a great method for handling console input. It only works if you know for 100% certain the thing you give it can be converted to an integer. It’s also for converting other things, like double
numbers, to integers. What you need is something that parses a string. That’s the fancy word we use for converting strings to numbers.
There is a TryParse()
method for every number type that lets you know if it worked or not. You use it like this:
I’d try and integrate it into your algorithm, but your use of single-letter variable names is a bummer. I don’t knwo what anything is, so I can’t tell what you’re doing without a lot of effort.
But the main problem I see right off the bat is there’s not a way you can make the user input only numbers to the console, so you HAVE to follow this process:
Get the input.
See if it’s a number.
If it isn’t, ask the user to try again or quit.
Proceed once you have a number.
C# devs
null reference exceptions