Home > database >  Powershell Basic
Powershell Basic

Time:03-01

I have to do a simple calculator , -, * and / on PowerShell for my coding introduction course. Why my Variable Valeur1 & Valeur2 don't remain intact during the integer validation ? My 4 options give a random answer and don't remember the initial value of my 2 Read-Host Variables.



Write-Host "Microsoft Powershell Calculator "

$R = $True
$OK = $True

while ($R -eq $True) {
    do {
        $Valeur1 = [int]::TryParse((Read-Host "Entrez une premiere valeur numerique"), [ref]$OK)
        if (-not $OK) {
            Write-Host ("ERREUR ! Vous devez entrez une valeur numerique")
            $OK = $false
        }
        else{
            $OK = $true    
        }
    } while (-not $OK)

    do {
        $Valeur2 = [int]::TryParse((Read-Host "Entrez une seconde valeur numerique"), [ref]$OK)
        if (-not $OK) {
            Write-Host ("ERREUR ! Vous devez entrez une valeur numerique")
            $OK = $false
        }
        else{
            $OK = $true    
        }
    } while (-not $OK)

    $operation = Read-Host -prompt "Quelle opération voulez-vous réaliser? Choisissez entre [ , -, *, or /] "
    Switch($operation)
    {
        -
        {
            Write-Host("Le resultat de votre soustraction est :")
            $Valeur1 - $Valeur2
        }
         
        {
            Write-Host("Le resultat de votre addition est :")
            $Valeur1   $Valeur2
        }
        *
        {
            Write-Host("Le resultat de votre multiplication est :")
            $Valeur1 * $Valeur2
        }
        /
        {
            Write-Host("Le resultat de votre division est :")
            $Valeur1 / $Valeur2
        }
        default
        {
            Write-Host("Option Invalide")
        }
    }

    $choix = Read-Host "Desirez-vous continuer? Y/N" 
    if($choix -eq "Yes"){
        $R = $true
    }
    if($choix -eq "Y"){
        $R = $true
    }
    else{
        $R = $false
        exit
    }
}

CodePudding user response:

From the Int32.TryParse(String, Int32) Documentation:

result Int32
When this method returns, contains the 32-bit signed integer value equivalent of the number contained in s, if the conversion succeeded, or zero if the conversion failed.

When you do [ref]$OK, if the conversion succeeds, this variable will hold the parsed result of your .TryParse operation, but, you're overwriting this value on your if and else conditions ($OK = $true and $OK = $false).

Furthermore, the output from the .TryParse method will be $true / $false depending on the success of the operation (this boolean will be assigned to $valeur1 and $valeur2 since both variables have assigned this operation).

Returns Boolean
true if s was converted successfully; otherwise, false.

Here is a simplified, working, variation of your code. It's worth mentioning that, since you're performing 2 parse operations, a function (ParseInput) holding the same logic would make more sense so that the code is not repeated.

Write-Host "Microsoft Powershell Calculator V3"

function ParseInput {
    $ref = ''
    do {
        $parsed = [int]::TryParse((Read-Host "Entrez une premiere valeur numerique"), [ref]$ref)
        if (-not $parsed) {
            Write-Host "ERREUR ! Vous devez entrez une valeur numerique"
        }
    } while (-not $parsed)
    $ref
}

$R = $true

while ($R) {
    $Valeur1 = ParseInput
    $Valeur2 = ParseInput
    $operation = Read-Host "Quelle opération voulez-vous réaliser? Choisissez entre [ , -, *, or /] "
    Switch($operation) {
        - {
            Write-Host "Le resultat de votre soustraction est :"
            $Valeur1 - $Valeur2
        }
          {
            Write-Host "Le resultat de votre addition est :"
            $Valeur1   $Valeur2
        }
        * {
            Write-Host "Le resultat de votre multiplication est :"
            $Valeur1 * $Valeur2
        }
        / {
            Write-Host "Le resultat de votre division est :"
            $Valeur1 / $Valeur2
        }
        default {
            Write-Host "Option Invalide"
        }
    }
    $choix = Read-Host "Desirez-vous continuer? Y/N"
    if($choix -notmatch "^(Yes|Y)$") {
        $R = $false
    }
}
  • Related