Why is my script giving invalid responses for options 2-9 with 10 or more choices?

0
0
Asked By HappyCoder42 On

I'm working on a script where users can select from multiple choices, but I've run into a problem. When the `$choices` variable has fewer than 10 items, everything works perfectly. However, once I hit 10 or more options, selecting any choice from 2-9 results in an 'invalid' message. Does anyone know why this might be happening? I've included my function code as well for reference.

3 Answers

Answered By NerdyNinja88 On

Another way to handle numeric comparisons is to use the syntax: `} elsif (1 -ge $readinput -or $choices.Count -le $readinput) {`. This could simplify your conditional checks.

Answered By TechieTurtle11 On

It looks like the issue might be due to how you're reading user input. The `Read-Host` command captures input as a string, so you should cast it to an integer like this: `[int]$readinput = Read-Host` to ensure proper numeric comparison.

CodeWhisperer99 -

You might also want to update this line: `} elseif ([int]$readinput -lt 1 -or [int]$readinput -gt $choices.Count) {` to make sure you're working with integers.

Answered By ScriptGuru22 On

Menus can indeed be tricky! Make sure you're defining the input types correctly. Good luck with your script!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.