So basically i created a function that displays a menu with different options ; here is the script
function Show-Menu
{
param (
[string]$Title = 'back to school'
)
Clear-Host
Write-Host "================ $title ================"
Write-Host "1: Press '1' to calculate the missing length of a triangle."
Write-Host "2: Press '2' to Calculate the area of a circle with a given diameter ."
Write-Host "3: Press '3' to Calculate the volume of a cylinder from a given diameter and height. ."
Write-Host "4: Press '4' to Calculate the volume of a sphere with a given diameter ."
write-host "5: Press '5' to Calculate the factorial of an integer ."
write-host "Q: Press 'Q' to exit"
$selection = Read-Host "Please make a selection"
switch ($selection)
{
'1' {
'You chose option #1'
} '2' {
'You chose option #2'
} '3' {
'You chose option #3'
} '4' {
'You chose option #4'
} '5' {'You chose option #5'
} 'q' { return
}
}
}
And I want to integrate this function in option #1 ; that's the function pythagore.
$length1=read-host "enter first length"
$length2=read-host "enter second length"
Function pythagore {
if ($Length1 -lt 1) { $Length1 = 1 }
if ($Length2 -lt 1) { $Length2 = 1 }
$length3 = ([math]::Sqrt(([math]::Pow($Length1, 2)) (([math]::Pow($Length2, 2)))))
write-host "The missing length is $length3"
}
How can i do that ?
CodePudding user response:
Start by parameterizing your function properly:
function pythagore {
param(
$Length1 = $(Read-Host "enter first length"),
$Length2 = $(Read-Host "enter second length")
)
if ($Length1 -lt 1) { $Length1 = 1 }
if ($Length2 -lt 1) { $Length2 = 1 }
$length3 = ([math]::Sqrt(([math]::Pow($Length1, 2)) (([math]::Pow($Length2, 2)))))
write-host "The missing length is $length3"
}
Then replace the string literal 'You chose option #1'
with a call to pythagore
:
switch($selection)
{
'1' {
pythagore
}
# ...
}
CodePudding user response:
Can you try :
Function pythagore ($Length1, $Length2)
{
if ($Length1 -lt 1) { $Length1 = 1 }
if ($Length2 -lt 1) { $Length2 = 1 }
$length3 = ([math]::Sqrt(([math]::Pow($Length1, 2)) (([math]::Pow($Length2, 2)))))
write-host "The missing length is $length3"
}
function Show-Menu
{
param (
[string]$Title = 'back to school'
)
Clear-Host
Write-Host "================ $title ================"
Write-Host "1: Press '1' to calculate the missing length of a triangle."
Write-Host "2: Press '2' to Calculate the area of a circle with a given diameter ."
Write-Host "3: Press '3' to Calculate the volume of a cylinder from a given diameter and height. ."
Write-Host "4: Press '4' to Calculate the volume of a sphere with a given diameter ."
write-host "5: Press '5' to Calculate the factorial of an integer ."
write-host "Q: Press 'Q' to exit"
$selection = Read-Host "Please make a selection"
switch ($selection)
{
'1' {
'You chose option #1'
break
} '2' {
$length1=read-host "enter first length"
$length2=read-host "enter second length"
pythagore $Length1 $Length2
break
} '3' {
'You chose option #3'
} '4' {
'You chose option #4'
} '5' {'You chose option #5'
} 'q' { return
}
}
}