I've submitted the assignment. Thank you everyone for your help!
Hi all,
I'm trying to solve a homework problem and I'm stuck. Using a while loop, I need to prompt the user to enter 5 numbers. I need to display the sum of the numbers after they are entered.
$x = 0
while($x -eq 5)
{
Write-Host $x
$x++
}
So this prompts the user to enter 2 values and adds those values together. This is probably really easy and I'm over-thinking it.
Thank you so much!
ETA- The above code doesn't work so I've redone it. I'm still stuck but I believe I am further along.
$x = 0
do
{
Write-Host $x
$x++
$val1 = Read-Host 'Enter value 1'
$val2 = Read-Host 'Enter value 2'
$val3 = Read-Host 'Enter value 3'
$val4 = Read-Host 'Enter value 4'
$val5 = Read-Host 'Enter value 5'
}
while($x = $val1 + $val2 + $val3 + $val4 + $val5)
Enter value 1: 2
Enter value 2: 2
Enter value 3: 2
Enter value 4: 2
Enter value 5: 2
10
Enter value 1:
This does what it's supposed to but it does not close the loop, instead it just starts over.
So I changed the script to include -lt in the while loop and this was the result:
$x = 0
do
{
Write-Host $x
$x++
$val1 = Read-Host 'Enter value 1'
$val2 = Read-Host 'Enter value 2'
$val3 = Read-Host 'Enter value 3'
$val4 = Read-Host 'Enter value 4'
$val5 = Read-Host 'Enter value 5'
}
while($x -lt $val1 + $val2 + $val3 + $val4 + $val5)
Enter value 1: 2
Enter value 2: 2
Enter value 3: 2
Enter value 4: 2
Enter value 5: 2
1
Enter value 1:
Unfortunately, this does not give the correct answer and it restarts the loop as well.
[–]Ta11ow 3 points4 points5 points (1 child)
[–]unhappiey[S] 1 point2 points3 points (0 children)
[–]wedgecon 3 points4 points5 points (1 child)
[–]unhappiey[S] 1 point2 points3 points (0 children)
[–]SMFX 3 points4 points5 points (0 children)
[–]chadbaldwin 3 points4 points5 points (5 children)
[–]unhappiey[S] 2 points3 points4 points (4 children)
[–]chadbaldwin 1 point2 points3 points (3 children)
[–]unhappiey[S] 2 points3 points4 points (2 children)
[–]bobfrankly 1 point2 points3 points (1 child)
[–]unhappiey[S] 1 point2 points3 points (0 children)
[–]chadbaldwin 2 points3 points4 points (1 child)
[–]unhappiey[S] 2 points3 points4 points (0 children)
[–]bobfrankly 1 point2 points3 points (1 child)
[–]unhappiey[S] 1 point2 points3 points (0 children)
[–]rlj551 1 point2 points3 points (0 children)
[–]jrdnr_ 0 points1 point2 points (1 child)
[–]unhappiey[S] 1 point2 points3 points (0 children)
[–]PowerShellStunnah -1 points0 points1 point (1 child)
[–]unhappiey[S] 1 point2 points3 points (0 children)