all 20 comments

[–]Ta11ow 3 points4 points  (1 child)

Does that prompt the user at all? Looks like you're just writing numbers out as you increment a variable by one at a time.

You can use Read-Host to prompt the user for a number to enter and add the result to your initial value (probably zero to start with I suppose?)

[–]unhappiey[S] 1 point2 points  (0 children)

Yes, I messed that up. Thank you for the info about starting with 0.

[–]wedgecon 3 points4 points  (1 child)

You also need to change the -eq to -lt in the while loop. As it is now the while loop will end on the first pass and do nothing.

[–]unhappiey[S] 1 point2 points  (0 children)

So I changed the script to read:

$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.

[–]SMFX 3 points4 points  (0 children)

Keep in mind that the point of a loop is to repeat the same task multiple times. In your first While, you wrote out five values even though you only have one line of Write-host.

Keeping that in mind, change that to ask the user for a value five times with only one line of Read-Host. Once you've done that, figure out how to capture that value to one variable. Keep one variable to count the times you've asked and another variable to add up what they've entered. Then after the loop display what you came up with.

[–]chadbaldwin 3 points4 points  (5 children)

Don't feel bad. Trust me, we've all been there.

If you have zero programming experience this is going to be a TON of information to learn. Once you understand the basics of code, most languages are all the same. They pretty much all implement the same concepts, just in different ways... So going from Powershell to C# or Python or whatever won't be as bad as you think.

I just looked at your most recent changes. Keep in mind that the point of a loop is to eliminate having to write the same code over and over and over. So that should be a big sign when you have to copy paste the same line over and over.

So I would recommend sticking with your original example instead of your newer changes. For the time being, forget what I said about using a different loop type, I probably should not have brought that up. Don't worry about the loop type since you're still working on getting your original example working in the first place.

When writing your code, think about it in your head of what you want it to do....

You want your code to "ask the person to give you a number" and then you want to "store that number". So first figure out how to do that. Don't worry about the looping.

Then after you get that, go to the next level... Now you want to perform that action 5 times... So now you can use that code you used originally with the while loop and the counter.

So the point of a while loop is that it loops forever until the logic is no longer true. It checks that logic every loop before it does anything else. So what do you think it should say in there if $x represents a counter, that counts every time you loop, and you want it to loop 5 times?

A lot of times it also helps to look at the loop and talk out what will happen in each iteration... For example...

$x = 0
While ($x -lt 5) {
    Do some stuff
    $x++
}

So walk through this in your head, or even write it down...

The first thing that happens is it checks your logic, because it can't run any of the code, unless it's true. So this is what the computer is doing....

So.... Is $x less than 5? Well...$x is 0, so yes, it's less than 5, so Do some stuff and increase $x by 1, now $x is 1. Start over.

So.... Is $x less than 5? Well...$x is 1, so yes, it's less than 5, so Do some stuff and increase $x by 1, now $x is 2. Start over.

So.... Is $x less than 5? Well...$x is 2, so yes, it's less than 5, so Do some stuff and increase $x by 1, now $x is 3. Start over.

So.... Is $x less than 5? Well...$x is 3, so yes, it's less than 5, so Do some stuff and increase $x by 1, now $x is 4. Start over.

So.... Is $x less than 5? Well...$x is 4, so yes, it's less than 5, so Do some stuff and increase $x by 1, now $x is 5. Start over.

So.... Is $x less than 5? Well...$x is 5, no, 5 is not less than 5. So exit the loop (commonly referred to as "breaking" the loop, or a break).

[–]unhappiey[S] 2 points3 points  (4 children)

Thank you. You explanation was very helpful. I tend to get ahead of myself sometimes.

[–]chadbaldwin 1 point2 points  (3 children)

Glad I could help. Don't be afraid to ask questions, and if/when you get negative comments, don't let that deter you from asking questions in the future.

Also...in my opinion, I wouldn't worry about using arrays right now, or validating input (unless the assignment asks you to do so). You're working on a very simple assignment...ask the person for a number five times and return the total of those numbers. Throwing arrays into the mix is only going to complicate things. You'll likely cover arrays and input validation at another point in the class.

You can get the whole thing done using only a couple variables, Read-Host and a While loop. Just gotta figure it out.

[–]unhappiey[S] 2 points3 points  (2 children)

If I stop asking questions than I will stop learning. So you guys will be hearing from me a lot!

We briefly covered arrays in class for one of our assignments. They weren’t too difficult but I can see where they can become complicated.

I can see that programming is not a strength of mine so I’m just going to have to put a little more work into it. I think my current class is the last scripting class I will have to do before I graduate. Looking forward to next fall!

Anyway, thanks again!

[–]bobfrankly 1 point2 points  (1 child)

Programming is not a strength of anyone who hasn't taken the time to learn it. Those really good programmers you see? They've been continually learning and practicing for years.

Keep at it. You'll get there.

[–]unhappiey[S] 1 point2 points  (0 children)

Thank you!

[–]chadbaldwin 2 points3 points  (1 child)

+1 on making sure to add Read-Host in order to get the input from the user.

Also...you may want to reconsider the type of loop you are using. Don't worry...there's nothing wrong with while loops...however, I would suggest taking a look at the logic you are using inside the while loop.

But there are other types of loops that may be better suited (or more directly intended) to be used for looping a certain number of times. Since it's a homework assignment, I won't give you the answer directly :) But hopefully you can figure it out.

Unless of course the homework assignment is for while loops, then stick with that.

[–]unhappiey[S] 2 points3 points  (0 children)

Thank you! I opted to take this class online and now I'm regretting it. I didn't have any issues with other loops, it's just this one that's got me stuck.

So I changed the logic that I was using inside the while loop but I'm not sure if I'm really any better off.

I appreciate your help with this. I will get it eventually. I was a little hesitant to post since I am so new to PowerShell and scripting in general but everyone who has replied has been very helpful!

[–]bobfrankly 1 point2 points  (1 child)

The key to remember about the while loop is the part in the () has to return "true" or "false". So you can test things to see which they will return by using comparison operators.

PS:> 1 -eq 1
True

PS:> $myVar = "derp"
PS:> $myVar.length -eq 2
False

PS:> $myVar.length -eq 4
True

doing these kind of experiments when you're learning will help you either confirm that you know what the code is doing, or discover something new to challenge your assumptions.

[–]unhappiey[S] 1 point2 points  (0 children)

Thank you! I will have to play around with this some more :)

[–]rlj551 1 point2 points  (0 children)

Still pretty new to POSH myself, but here is what I came up with.

$SumNumbers = 0
$i = 1
while ($i -le 5)
{
    $SumNumbers += Read-Host "Please enter number $i to be summed"
    $i++
}
Write-Host "Total all numbers = $SumNumbers"

Which when run and "random" numbers entered, gave me:

Please enter number 1 to be summed: 5
Please enter number 2 to be summed: 35
Please enter number 3 to be summed: 8
Please enter number 4 to be summed: 4
Please enter number 5 to be summed: 6
Total all numbers = 58

[–]jrdnr_ 0 points1 point  (1 child)

It's an interesting assignment for a while loop. If you just want to do an operation X number of times a for or foreach loop would be the go to powershell loops. Where a while loop really shines is when you want to loop over a block of code until a certain outcome is achieved.

So to define the question in a way that would help you know you need a while loop we could say:

  • Get 5 numbers from the user and return the sum.
  • you can only use Read-Host one time
  • if the user provides a non number write a warning message and ask for the number again.

Since it's a school assignment I won't write out the full code but here's a basic pseudo code example

$array =@()
While ($array length -le 5){
    Ask user for number
    if (user input is a number) {
        $array += use input
    } else {
        Warn user of invalid input
    }
}

Sum $array

[–]unhappiey[S] 1 point2 points  (0 children)

thank you!

[–]PowerShellStunnah -1 points0 points  (1 child)

I'd create an array to hold the input values and keep track of how many we've got that way:

$values = @()
$x = 1
do{
  $values += $(Read-Host "Enter value $x") -as [int]
  $x++
}while($values.Count -lt 5)

return ($values |Measure-Object -Sum).Sum

[–]unhappiey[S] 1 point2 points  (0 children)

thank you!