I get this message when running my script, please could you advise how i can overcome the issue so that i should be able to quit the game at any point?
Cannot convert value "Q" to type "System.Int32". Error: "Input string was not in a correct format."
At C:\Users\F\Documents\Scripts\PowerShell\Test.ps1:58 char:7
+ $guess = Read-Host " Enter a number between 1 and 1000 or Press Q to quit ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException
+ FullyQualifiedErrorId : RuntimeException
My Script is here
--------------------------------------------------------------------------------------------------------------------
# *************************************************************************
#
# Script Name: GuessMyNumber.ps1 (The Guess My Number Game)
# Version: 1.0
# Author: Captedgar
# Date: October 10, 2008
#
# Description: This PowerShell script challenges the player to attempt to guess a randomly generated number in the range of 1 to 100 in as few guesses as possible.
#
# *************************************************************************
Clear-Host
$number = 0 # Keeps track of the game's secret number
$noOfGuesses = 0 # Keeps track of the number of guesses made
$noOfLowGuesses = 0 # Keeps track of the number of Low guesses made
$noOfHighGuesses = 0 # Keeps track of the number of High guesses made
$playGame = "Yes" # Controls when to quit the game
$playGamesNumber = 1# Keeps track of number of games played
$randomNo = New-Object System.Random # This variable stores a random object
$status = "Play" # Controls the current round of play
$guess = "" # Stores the player's guess
$reply = "" # Stores the player's response when asked to play again
Write-Host "`n`n`n`n`t W E L C O M E T O T H E G U E S S M Y"
Write-Host "`n`n`n`t`t`tN U M B E R G A M E"
Write-Host "`n`n`n`t`t`tBy Capt Edgar"
Write-Host "`n`n`n`n`n`n`n`n`n`n Press Enter to continue."
Read-Host
Clear-Host
Write-Host "This game is to guess a number which is randomly generated by the Computer."
Write-Host "This number would be between 1 to 100."
Write-Host "Please Choose a number to guess. The Computer would then tell you whether"
Write-Host "your number is greater or lesser than the randomly generated number."
Write-Host "The Game terminates when you have guessed the correct number at which you"
Write-Host "would be presented by the correct number and the amount of guesses you have"
Write-Host "taken. Just before closing the game, you are presented with an option to play"
Write-Host "the game again. If you choose No then the game fully terminates"
Write-Host "`n`n`n"
Read-Host "Press continue"
Clear-Host
while ($playGame -ne "No")
{
[int]$number = $randomNo.Next(1,101)
Write-Host $number
Clear-Host
while ($status -ne "Stop")
{
while ($guess -eq "")
{
Clear-Host
Write-Host
$guess = Read-Host " Enter a number between 1 and 1000 or Press Q to quit the game"
}
if([str]$guess = [str]"Q")
{
$status = "Stop"
$playGame = "No"
}
$noOfGuesses++
if ([int]$guess -lt $number)
{
Clear-Host
Write-Host "`n Sorry. Your guess was too low. Press Enter to guess again."
$guess = ""
$noOfLowGuesses++
Read-Host
}
else{
if ([int]$guess -gt $number)
{
Clear-Host
Write-Host "`n Sorry. Your guess was too high. Press Enter to guess again."
$guess = ""
$noOfHighGuesses++
Read-Host
}
else
{
Clear-Host
Write-Host "`n Congratulations. You guessed my number! Press Enter to continue."
$status = "Stop"
Read-Host
}
}
}
if([str]$guess = [str]"Q")
{
Clear-Host
Write-Host "`n Game Statistics"
Write-Host " -----------------------------------------------------------"
Write-Host "`n The secret number was: $number."
Write-Host "`n You guessed it in $noOfGuesses guesses.`n"
Write-Host "`n You have played this game" $playGamesNumber "times.`n"
Write-Host "`n Number of Low Guesses was " $noOfLowGuesses "times.`n"
Write-Host "`n Number of High Guesses was " $noOfHighGuesses "times.`n"
Write-Host " -----------------------------------------------------------"
Write-Host "`n`n`n`n`n`n`n`n`n`n`n`n`n`n Press Enter to continue."
Read-Host
Clear-Host
$reply = ""
while ($reply -eq "")
{
Clear-Host
Write-Host
$reply = Read-Host " Would you like to play again? (Y/N) "
if (($reply -ne "Y") -and ($reply -ne "N"))
{
$reply = ""
}
}
if ($reply -eq "Y")
{
$number = 0
$noOfGuesses = 0
$status = "Play"
$playGamesNumber++
$noOfLowGuesses = 0
$noOfHighGuesses = 0
$guess = 0
}
else
{
$playGame = "No"
}
}
}
Clear-Host
--------------------------------------------------------------------------------------------------------------------