Home > Back-end >  Need assistance with powershell script
Need assistance with powershell script

Time:03-03

See code below ##If user inputs 0 or a100 or non-numeric value for temp, I need to display "Error: You must enter a numeric Value!" Write-host $("Error: You must enter a numeric Value!") ## then goes back to start ##im guessing if ($value isnot [int]) or something like that for it to work. thank you. ##Error I get without proper coding......What is the temperature in Fahrenheit: a111 ##Cannot convert value "a111" to type "System. Single". Error: "Input string was not in a ##correct format."

Write-Host("="*31)
Write-Host $("Fahrenheit to Celsius Converter")
Write-Host $("Script Written By Jesse  ")
Write-Host("="*31)

$value = (Read-Host("What is the temperature in Fahrenheit"))
$fahr = (($value  -32) /9) *5
Write-Host $("Fahrenheit", $value, "is equal to Celsius:", [Math]::Round($fahr,3))
$input = (Read-Host("Do you want to convert another Fahrenheit value? (1 = yes, 0 = no)?"))
Write-Host ("="*31)
        

while ($input)
{
    if ($input -eq 1)
    {
        Write-Host $("Fahrenheit to Celsius Converter")
        Write-Host $("Script Written By Jesse Nieto ")
        Write-Host ("="*31)
        $value = (Read-Host("What is the temperature in Fahrenheit"))
        $fahr = (($value -32) /9) *5
        Write-Host $("Fahrenheit", $value ,"is equal to Celsius:" ,[Math]::Round($fahr,3))
        $input = (Read-Host("Do you want to convert another Fahrenheit value? (1 = yes, 0 = no)?"))
        Write-Host("="*31)
    }
    elseif ($input -eq 0)
    {
        Write-Host $("Thank You. Bye! ")
        break
    }
    else
    {
        Write-Host $("Please enter a valid option! ")
        $input = (Read-Host ("Do you want to convert another Fahrenheit value? (1 = yes, 0 = no)?"))
        Write-Host ("="*31)
}
}

CodePudding user response:

There is much redundant code on your script which could be simplified, the biggest issue is the use of $input which is an automatic variable and should not be assigned manually. As for the formula to convert from Fahrenheit to Celsius, you could use a function for that, as aside, according to Google, the formula should be (X − 32) × 5 / 9 though I'm not an expert on this, feel free to change for whatever you like.

function ConvertTo-Celsius {
    param([int]$Value)
    ($value - 32) * 5 / 9
}

:outer while($true) {
    $value = Read-Host "What is the temperature in Fahrenheit"
    try {
        $celsius = ConvertTo-Celsius $value
    }
    catch {
        # If input was invalid restart the loop
        Write-Host "Invalid input, only integers allowed!"
        continue
    }

    Write-Host "Fahrenheit $value°F is equal to $([math]::Round($celsius, 3))°C"
    do {
        $shouldExit = Read-Host "Do you want to convert another Fahrenheit value? (1 = yes, 0 = no)?"
        switch($shouldExit) {
            0 {
                Write-Host "Thank You. Bye!"
                break outer
            }
            1 { continue }
            Default { Write-Host "Please enter a valid option!" }
        }
    } until($shouldExit -eq 1)
}
  • Related