Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - CTP2 comparison operators

Reply
 
Old 08-29-2008   #1 (permalink)
Steve


 
 

CTP2 comparison operators

In the following code, a comparison against a random number by an integer
yields an incorrect result. If the random number is 500, entering a 4 yields
a correct result, but entering a 6 yields IF(6 -gt 500) as TRUE.


$Random=Get-Random -min 1 -max 1000
$Guess=0
While ($Guess -ne $Random)
{
$Guess=Read-Host "Please enter your guess (Enter ZERO to quit)"
$Guess
$Random
if ($Guess -eq 0) {Write-host "Buh-Bye";Break}
If ($Guess -lt $Random) {Write-Host -foregroundcolor RED "Too LOW!!!"}
If ($Guess -gt $Random) {Write-Host -foregroundcolor RED "Too HIGH!!!"}
If ($Guess -eq $Random) {Write-host -foregroundcolor RED "You Got
it!!!";Break}
}

Did I do something wrong or is there a bug somewhere?



My System SpecsSystem Spec
Old 08-30-2008   #2 (permalink)
RichS [MVP]


 
 

RE: CTP2 comparison operators

I think the problem is with read-host it returns the number yoiu type as a
string. $guess is a string rather than an integer so you are comaring a
string to an integer which is where the odd behaviour comes from. Change
your script to

$Random=Get-Random -min 1 -max 1000
$Guess=0
While ($Guess -ne $Random)
{
[int]$Guess=Read-Host "Please enter your guess (Enter ZERO to quit)"
$Guess
$Random
if ($Guess -eq 0) {Write-host "Buh-Bye";Break}
If ($Guess -lt $Random) {Write-Host -foregroundcolor RED "Too LOW!!!"}
If ($Guess -gt $Random) {Write-Host -foregroundcolor RED "Too HIGH!!!"}
If ($Guess -eq $Random) {Write-host -foregroundcolor RED "You Got
it!!!";Break}
}

and it should work. You could also change the if statements to a switch for
maximum efficiency

--
Richard Siddaway
All scripts are supplied "as is" and with no warranty
PowerShell MVP
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Steve" wrote:
Quote:

> In the following code, a comparison against a random number by an integer
> yields an incorrect result. If the random number is 500, entering a 4 yields
> a correct result, but entering a 6 yields IF(6 -gt 500) as TRUE.
>
>
> $Random=Get-Random -min 1 -max 1000
> $Guess=0
> While ($Guess -ne $Random)
> {
> $Guess=Read-Host "Please enter your guess (Enter ZERO to quit)"
> $Guess
> $Random
> if ($Guess -eq 0) {Write-host "Buh-Bye";Break}
> If ($Guess -lt $Random) {Write-Host -foregroundcolor RED "Too LOW!!!"}
> If ($Guess -gt $Random) {Write-Host -foregroundcolor RED "Too HIGH!!!"}
> If ($Guess -eq $Random) {Write-host -foregroundcolor RED "You Got
> it!!!";Break}
> }
>
> Did I do something wrong or is there a bug somewhere?
>
>
>
My System SpecsSystem Spec
Old 08-30-2008   #3 (permalink)
Steve


 
 

Re: CTP2 comparison operators


"RichS [MVP]" <RichSMVP@xxxxxx> wrote in message
news:13303330-2522-4438-A913-06447A3B5E93@xxxxxx
Quote:

>I think the problem is with read-host it returns the number yoiu type as a
> string. $guess is a string rather than an integer so you are comaring a
> string to an integer which is where the odd behaviour comes from.
I thought it was something like that. Thanks much!


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
redirection operators as syntactic suger PowerShell
Powershell Operators PowerShell
Subexpressions and Unary Operators PowerShell
PowerShell User Guide - Comparison Operators - error? PowerShell
Bit Operators PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46