-2

i have a integer input that the user supposed to enter. how can i protect my program from input of 01 instead of 1. my input is integer !

choice = int.Parse(Console.ReadLine());       
if (choice < 1 || choice > 3)
{
    v = true;
    Console.WriteLine("Please enter number between 1 - 3: ");
}
Cid
  • 14,968
  • 4
  • 30
  • 45
  • 1
    check the length, since it should always be 1, or compare to an array of valid input such as [1,2,3] – Josh Adams Jan 08 '19 at 14:13
  • only 01 or anything that starts a zero like: 012, or 0123, 00001 ? – styx Jan 08 '19 at 14:14
  • 2
    For *numbers* it doesn't matter what input was, only the parsed value matter. Are you trying to do masked input (e.g. phone number) or numbers? – Sinatr Jan 08 '19 at 14:14
  • i tried to make sure the user entered only numbers between 1-3 and not 01 or 02 or 03 because now the program accepted this issue – Shahar Dvora Jan 08 '19 at 14:16
  • 3
    What's wrong with 01? How can it cause an issue since it's converted into a numeric value? – Cid Jan 08 '19 at 14:18
  • 3
    As soon as you `int.Parse()` you lose the ability to do what you're trying to do. Don't convert it - just leave it as a string. Once you've validated it then you can convert it and do whatever you want. Also, `Console.ReadKey()` might be more suitable since that will only allow 1 character and not wait for enter to be pressed. – Reinstate Monica Cellio Jan 08 '19 at 14:18
  • 1
    If user prompted to enter number, he can enter `01` or `+00000000001`. Both should be *OK* if you allow number `1`. Because after parsing both will be `1`. – Sinatr Jan 08 '19 at 14:20
  • and if the input is integer can i do something else to solve that. – Shahar Dvora Jan 08 '19 at 14:22
  • @Sinatr i know but can i not seen it in the console ? – Shahar Dvora Jan 08 '19 at 14:24
  • Solve what? You get input as `string` (this is what `Console.ReadLine()` returns), don't parse that to `int`. For a single number the value of string `Length` has to be 1, and this character (use indexer to get `char` from `string`) [IsNumber()](https://learn.microsoft.com/en-us/dotnet/api/system.char.isnumber?view=netframework-4.7.2) should return `true`. Of if you want character from `'1'` to `'2'`, then stick to `char` in your `if`. – Sinatr Jan 08 '19 at 14:28
  • You can [hide input](https://stackoverflow.com/q/23433980/1997232). – Sinatr Jan 08 '19 at 14:29
  • @Sinatr my input is integer because of that i used - int.Parse and if i change the input to string it will affect on other things. – Shahar Dvora Jan 08 '19 at 14:34
  • Don't use `int.Parse`. – Sinatr Jan 08 '19 at 14:35

1 Answers1

0

If you really want to prevent the user inputting 01, etc, you can just evaluate the string.

switch(Console.ReadLine()) {
    case "1": //do something
        break;
    case "2": //do something else 
        break;
    case "3": //do a third thing
        break;
    default: 
        v = true;
        Console.WriteLine("Please enter number between 1 - 3: ");
        break;
}

But as others already said in the comments, it shouldn't really matter if the user puts 01 instead of 1.

Piflik
  • 333
  • 1
  • 10