-2

I'm completely lost on some homework. The assignment is to use a while loop and prompt the user to enter 5 numbers. My current code looks like the following:

$x = 1
do 
{
    Write-Host 'Enter 5 numbers'
    $x++
} while ($x -eq 5) 

Seems like issue is somewhere in here ($x -eq "5"). I want PowerShell just to prompt the user for 5 random numbers then get the sum of those numbers.

Seth
  • 1,215
  • 15
  • 35
  • You should post what you have tried already - people are more likely to help if they don't feel like they are just doing your homework for you – Bassie Mar 11 '18 at 02:54
  • Welcome to StackOverFlow. StackOverFlow does not like questions that ask us to do your homework when you have not displayed an effort to solve your problem. You should explain what you have tried, what went wrong and then ask for help on fixing what went wrong. See [this help page](https://stackoverflow.com/help/on-topic) for more information. – Tyler Mar 11 '18 at 02:55
  • PS do { >> $response = Read-Host "Enter 5 numbers" >> } until ($response -eq "5") Enter 5 numbers: 10 Enter 5 numbers: 10 Enter 5 numbers: 10 Enter 5 numbers: 10 Enter 5 numbers: 10 Enter 5 numbers: 10 Enter 5 numbers: 10 Enter 5 numbers: 10 – Jose Del Sol Mar 11 '18 at 02:55
  • @JoseDelSol You can edit your original post by clicking on the "edit" link. – Tyler Mar 11 '18 at 02:56
  • @JoseDelSol I believe [this question](https://stackoverflow.com/questions/8184167/prompt-for-user-input-in-powershell) might be of help to you. – Tyler Mar 11 '18 at 03:25

2 Answers2

0

A homework assignment. 8^} Oh well... Here is one way to do this.

Yet, taking your original post and what you show in your comment. Your issue is, you are not capturing anything to sum up, nor are you using a counter to meet you entry limit.

Clear-Host
# Initial variables

$ResponseCount = 0
$Total = 0

do {
    # Get a user response
    $response = Read-Host "Enter 5 numbers"

    # increment response counts
    $ResponseCount ++

    # capture and sum the entered numbers
    $Total += $response
} 
until ($ResponseCount -eq "5") 
"`nYou have completed the required 5 entries"
"The sum of the entered numbers is: $Total"


Enter 5 numbers: 10
Enter 5 numbers: 20
Enter 5 numbers: 30
Enter 5 numbers: 40
Enter 5 numbers: 50

You have completed the required 5 entries
The sum of the entered numbers is: 150
postanote
  • 15,138
  • 2
  • 14
  • 25
  • He actually is using a counter but with his chosen loop type the condition for the loop is wrong. In this code `$x` would equal to `$ResponseCount` in your example. – Seth Mar 12 '18 at 12:16
0

You're correct that the issue is your condition for the loop. Currently you're telling PowerShell to run the loop while $x is exactly five. If you want your loop to properly work you will have to change the condition. You can check the PowerShell help for about_Comparison_Operators to get an idea about the operators that are available.

The other thing you need to do is read the actual values and save whatever the user enters to build the sum. There are quite a few approaches to this and the other answer has a solution for that. Also consider using more expressive variable names to make it easier for someone else to follow your code (though that's not so much an issue for this short example).

Seth
  • 1,215
  • 15
  • 35