# Problem with number guessing game

Page 1 of 1

## 4 Replies - 2787 Views - Last Post: 03 November 2007 - 12:10 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=36672&amp;s=ea238f5b8d563553c55a8b7d6abd1096&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Smarf

Reputation: 6
• Posts: 80
• Joined: 21-September 07

# Problem with number guessing game

Posted 02 November 2007 - 07:00 PM

My assignment is to write a number guessing game for VB console with the following:

- Ask them if they want to play (Y or N)
- If they say yes then generate a random number 1 - 10
- Use a function to get their integer
- Generate a random number
- Compare the two using a second function
If it's incorrect say "Too high" or "Too low" and let them guess again
If it's correct say "Correct" and ask if they want to play again

Yes I know it's odd but for some reason the professor wanted the actual random number generated at the beginning after they say yes AND displayed on the screen. Maybe for testing purposes, so I put it in.

Here is my code, and I can't figure out why it runs "Too high" five or six times on the screen. I'm taking C++ at the same time and am way more familiar with it's syntax so I'm guessing I'm doing my if else statements wrong or something.

```'This program generates a random number
'and the let's the player guess until
'they get it right.
'Created by Smarf on 11-2-07

Option Strict On

Module Module1

Sub Main()

'Declare variables
Dim guess As Integer = 0
Dim playGame As String = " "
Dim correct As Boolean = False

'Set up random object so we can generate random number letter
Dim numRandom As Integer = 0
Dim randomObject As New Random()

'Ask if person wants to play
Console.Write("Do you want to guess a number? (Y/N): ")

'Generate and display random number 1 - 10
numRandom = randomObject.Next(1, 11)
Console.WriteLine(numRandom)

'If they do want to play...
While playGame.ToUpper() = "Y"

'Get guess from user
guess = getNumber()

'Compare guess to random number
correct = compare(guess, numRandom)
End While

End Sub

'This subroutine gets the number from the user
Function getNumber() As Integer
Dim myGuess As Integer
Console.Write("Enter an integer number between 1 - 10: ")
Return myGuess
End Function

'Compare guess to random number
Function compare(ByVal myGuess As Integer, ByVal myRandom As Integer) As Boolean

Dim playGame As String = " "
'If they get it right
If (myGuess = myRandom) Then
Console.WriteLine("Correct!")
Console.WriteLine("Play again? (Y/N)")
End If

'If guess is too high
If (myGuess > myRandom) Then
Console.Write("Too high...")
Return False
End If

'If guess is to low
If myGuess < myRandom Then
Console.Write("Too low...")
Return False
End If

End Function

End Module
```

One more thing, I'm not really comfortable with Boolean types yet as far as syntax goes, so that may be it too.

Thanks for any help,
Smarf

Is This A Good Question/Topic? 0

## Replies To: Problem with number guessing game

### #2 PsychoCoder

Reputation: 1659
• Posts: 19,853
• Joined: 26-July 07

## Re: Problem with number guessing game

Posted 02 November 2007 - 07:57 PM

The reason it was writing so many times for each number entered is because your getNumber Function. here is how your functions looks as posted:

``` 'This subroutine gets the number from the user
Function getNumber() As Integer
Dim myGuess As Integer
Console.Write("Enter an integer number between 1 - 10: ")
myGuess = Console.Read()  <= This is your problem, it should be ReadLine()
Return myGuess
End Function

```

I pointed our your error in the code, you're using Read() instead of what you should be using ReadLine(). Read() reads everything on the screen, while ReadLine() reads just the single line that is currently entered. Also, you have variables in this function you dont really need, myGuess really isnt needed, as you'll see in the changes Ive made to that function:

```''' <summary>
''' This subroutine gets the number from the user
''' </summary>
''' <returns>The number entered by the user</returns>
''' <remarks></remarks>
Function getNumber() As Integer
'Write your message to the console
Console.Write("Enter an integer number between 1 - 10: ")
'Return the number they entered (using ReadLine, not Read)
End Function

```

Now that we have your error out of the way, Ive done some cleaning up of your application, and here is it

```Module Module1
''' <summary>
''' Main method in the application,
''' this is the entry point into the application
''' </summary>
''' <remarks></remarks>
Sub Main()
'Declare variables
Dim guess As Integer = 0
Dim playGame As String = String.Empty
Dim correct As Boolean = False

'Set up random object so we can generate random number letter
Dim numRandom As Integer = 0
Dim randomObject As New Random()

'Ask if person wants to play
Console.Write("Do you want to guess a number? (Y/N): ")

'Generate and display random number 1 - 10
numRandom = randomObject.Next(1, 11)
'Write the random number to the screen
Console.WriteLine(numRandom)

'If they do want to play...
'Loop until they enter the correct answer
While playGame.ToUpper() = "Y"
'Get guess from user
guess = getNumber()
'Compare guess to random number
correct = compare(guess, numRandom)
End While

End Sub

''' <summary>
''' This subroutine gets the number from the user
''' </summary>
''' <returns>The number entered by the user</returns>
''' <remarks></remarks>
Function getNumber() As Integer
'Write your message to the console
Console.Write("Enter an integer number between 1 - 10: ")
'Return the number they entered (using ReadLine, not Read)
End Function

'Compare guess to random number
''' <summary>
''' Function to compare the users guess
''' with the random number generated
''' </summary>
''' <param name="myGuess"></param>
''' <param name="myRandom"></param>
''' <returns></returns>
''' <remarks></remarks>
Function compare(ByVal myGuess As Integer, ByVal myRandom As Integer) As Boolean
'Variable to hold the users answer
Dim playGame As String = String.Empty
'If they get it right
If (myGuess = myRandom) Then
Console.WriteLine("Correct!")
Console.WriteLine("Play again? (Y/N)")
Return True
End If

'If guess is too high
If (myGuess > myRandom) Then
Console.Write("Too high...")
Return False
End If

'If guess is to low
If myGuess < myRandom Then
Console.Write("Too low...")
Return False
End If
End Function
End Module

```

Hope this helps

### #3 Smarf

Reputation: 6
• Posts: 80
• Joined: 21-September 07

## Re: Problem with number guessing game

Posted 03 November 2007 - 08:20 AM

PsychoCoder, on 2 Nov, 2007 - 07:57 PM, said:

Hope this helps

Thanks for the help, it did help. I still have a few logic errors to work on about continuing the game each time but I'm going to play around with it before I ask for help again.

We're required to use Option Strict On so the Console.ReadLine() wouldn't work since it was taking in a string but saving it as an integer. Thankfully the IDE suggested these conversion type syntax thing:

### #4 PsychoCoder

Reputation: 1659
• Posts: 19,853
• Joined: 26-July 07

## Re: Problem with number guessing game

Posted 03 November 2007 - 09:03 AM

For starters, this is VB.Net and Cint is a VB6 function, so try using this myGuess = CInt(Console.ReadLine). CType is the .Net replacement for CInt. As for your second problem or not generating a new random number when they chose to play again.

What I did was move your Dim numRandom As Integer = 0 and Dim randomObject As New Random() outside the Main sub and made them global. I then created a 3rd global Private rand As Integer. You had your random number generating logic inside the Main sub, therefore only generating a random number the first time they chose Y to place, so I made it its own function, which means it will generate a random number anytime we call it, like so:

```Private Function GetRandomNumber() As Integer
'Generate and display random number 1 - 10
numRandom = randomObject.Next(1, 11)
'Write the random number to the screen
Console.WriteLine(numRandom)
Return numRandom
End Function

```

This function returns your random number when its called and writes it to the console (you were only writing the random number on the initial play, not on subsequent plays) . In your main sub I added these lines of code, to check and make sure they selected Y and to generate the random number:

```'Make sure they want to play the game
If playGame.ToUpper = "Y" Then
'Generate the random number
rand = GetRandomNumber()
End If

```

Finally, in your compare function, where you have If (myGuess = myRandom) Then I call the GetRandomNumber again to generate a new random number:

```'If they get it right
If (myGuess = myRandom) Then
Console.WriteLine("Correct!")
Console.WriteLine("Play again? (Y/N)")
'Generate a new random number
rand = GetRandomNumber()
Return True
End If

```

Hope that helps

### #5 Smarf

Reputation: 6
• Posts: 80
• Joined: 21-September 07

## Re: Problem with number guessing game

Posted 03 November 2007 - 12:10 PM

Thanks again, I got it working the way I wanted.