已答覆 Forcing numeric input Powershell

  • Thursday, November 29, 2012 7:06 PM
     
      Has Code

    Hello guys, 

    I am a complete novice in Windows Powershell and I am writing a simple calculator script where people put in 2 numbers (Stored as $Value1 and $Value2) and those get multiplied, divided, subtracted and whatnot. The problem however is that people can still input letters for example, while I only want them to input numbers. Also, when people write down a letter and hit Enter, I want them to receive a message that the Input is incorrect.

    Is there any way to achieve this in Windows PowerShell? And if yes, would you mind putting it in a simple way for me here ?

    Much appreciated.

    I'll include the code that I wrote below so it might be easier to understand my problem:

    clear-host Write-Host "" Write-Host "--------------------------------------------------------" -for cyan Write-Host "-----------------SCRCON W1 Input script-----------------" -for cyan Write-Host "------------------Input & calculation-------------------" -for cyan Write-Host "--------------------------------------------------------" -for cyan Write-Host "" Write-Host " Press Enter to continue." #Pause script execution and wait for the user to press the Enter key Read-Host

    #Define the variables used in this script to collect player input $Value1 = "" #Stores the value to calculate with supplied by the user $Value2 = "" #Stores the value to calculate with supplied by the user #Provide the user with instructions Write-Host " This is an interactive simple calculator. " -for cyan Write-Host Write-Host " Please input the values to be calculated with. " -for cyan Write-Host Write-Host " Press the Enter key to continue." #Pause script execution and wait for the user to press the Enter key Read-Host #Ask the user the first question while ($Value1 -eq ""){ Write-Host $Value1 = read-host " Enter the first value " } #Ask the user the second question while ($Value2 -eq ""){ Write-Host Write-Host $Value2 = read-host " Enter the second value " } Write-Host "" Write-Host "--------------------------------------------------------" -for cyan Write-Host " Adding your values up gives:" -for cyan [int]$Value1 + [int]$Value2 Write-Host " Multiplying your values gives: " -for cyan [int]$Value1 * [int]$Value2 Write-Host " Subtracting your values gives: " -for cyan [int]$Value1 - [int]$Value2 Write-Host "--------------------------------------------------------" -for cyan #Pause script execution and wait for the user to press the Enter key Read-Host

    Greetings, Siem


All Replies

  • Thursday, November 29, 2012 7:21 PM
    Moderator
     
     Answered Has Code

    Hi,

    You can use -as to convert the input string to a Double. For example:


    do {
      write-host -nonewline "Enter a numeric value: "
      $inputString = read-host
      $value = $inputString -as [Double]
      $ok = $value -ne $NULL
      if ( -not $ok ) { write-host "You must enter a numeric value" }
    }
    until ( $ok )
    
    write-host "You entered: $value"
    

    Bill

    • Marked As Answer by SiemHermans Thursday, November 29, 2012 7:34 PM
    •  
  • Thursday, November 29, 2012 7:34 PM
     
     

    That is great! Thanks alot Bill. Problem is however, I don't know why it worked and I would really like to know. 

    I like what the -nonewline does. I think the $inputString refers to the numeric input of the host ? Furthermore; What does the -as [Double] do?

    Thanks alot either way!

  • Thursday, November 29, 2012 7:48 PM
    Moderator
     
     

    Hi,

    Start with Learn link at the top of this forum.

    Bill