Random Number Guessing Game

FIR93

New member
Joined
Nov 24, 2012
Messages
4
Programming Experience
Beginner
I don't understand where I am doing something wrong. The instructions is to create a random application that generates a random number in the range of 1 through 100. If it's too low state "too low, try again" and the same for if it's too high.

(I'm using visual basic)

So I declared my variables and wrote a code for the procedure. I declared x as my integer. Any advice on how to correct it?
Thanks for all help

intNum = rand.Next(100) + 1
For
For x > intNum
MsgBox("Too high, try again")
Next
For x < intNum
MsgBox("Too low, try again")
Next
For x = intNum
MsgBox("You guessed right!")
Next
Next
r
 
I don't recognize your use of "For Next" so I will assume it is from an older vb version.

In the interest of reference, here is one way to do what you want in vb.net:

Dim rand As New Random
   Dim intNum As Integer = rand.Next(1, 101)

   Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

      Try

         Dim x As Integer = Convert.ToInt16(TextBox1.Text)


         Select Case x

            Case intNum

               MsgBox("You guessed right!")


            Case intNum + 1 To 100

               MsgBox("Too high, try again")
               TextBox1.Text = ""

            Case 1 To intNum - 1

               MsgBox("Too low, try again")
               TextBox1.Text = ""

            Case Else

               MsgBox("Your number must be between 1 and 100!")
               TextBox1.Text = ""

         End Select



      Catch ex As Exception
         MessageBox.Show("Please ensure you enter a whole number between 1 - 100")
         TextBox1.Text = ""
         Exit Sub
      End Try



   End Sub
 
If you use a textbox for the user's guess, then you cannot use a loop because it will never end. You need to declare the random number at form level, above the Sub. The user will have to click the button every time he makes a new guess. After he guesses correctly, you will have to reset the random number to a new value for the next game. Otherwise, every game you restart will use the same random value unless you end the program.

If you declare the random number inside the sub, then you will get a different random number every time the button is clicked, but the game will never end unless the user guesses it right the very first time. This time, you will need to use a loop to keep the same random number until the game ends. However, the loop will never end unless you use an InputBox for the user's guess.

You do not need to use Convert.To or a Try Catch block for the conversion. Use the TryParse() method instead, which combines it all into a single statement. The first argument is the string to convert from; the second is the number to convert into. If you're using a textbox:

Dim guess As Integer
Integer.TryParse(TextBox1.Text, guess)
or
Dim guess As Integer, sgess As String
sgess = TextBox1.Text
Integer.TryParse(sgess, guess)

If you're using an input box:

Dim sgess As String
sgess = InputBox("Enter your guess")
Integer.TryParse(sgess, guess)

I have several different versions of a guessing game worked out. However, this looks like a homework assignment, so I am not posting the code. Please work it out for yourself and post your code if you still need help.
 
What is wrong with convert.to inside a try block? I wanted to incorporate corrective instructions if the user is entering in the wrong type of format i.e decimal number or non-numeric characters.
 
Dim number, response As String
number = 7
Do Until response = number
response = InputBox("Enter Number")
If response <> number Then
MsgBox("Too high, try again")
Else
MsgBox("Hooray!")


End If

Loop

TextBox1.Text = ""


This is what I have so far, and it's not homework it's extra credit.
 
22Degrees: TryParse returns a Boolean value. If False, it returns 0 without an error. If True, conversion was successful and it returns the number. You can easily inform the user if the input is wrong:

Dim ok As Boolean
ok = Integer.TryParse(sgess, guess)
If ok = False Then MessageBox.Show("Wrong input. Please reenter")

or

If Integer.TryParse(sgess, guess) Then
'proceed with program
Else
MessageBox.Show("Wrong input")
'etc.
End If


FIR93: I see something very wrong with your code. You declared response as String and assigned a number to it, without enclosing it inside quotation marks. That is very poor programming and will cause a lot of errors along the way.
I strongly suggest you turn Option Strict On. This will prevent implicit conversions. Place the command at the very top of your code:
Option Strict On

You also need to convert the string into a number in order to compare it with another number.

Dim response As String
Dim number, guess as Integer
number = 7 'This is the secret number, which should be random
Do
response = TextBox1.Text 'This is the user's guess
Integer.TryParse(response, guess) 'convert the string guess into a number
'display messages to the user if guess is too low or too high
Loop Until guess = number

Note: Instead of TextBox1.Text = "" you can do TextBox1.Clear()
 
Last edited:
EDIT: Took my time replying and Solitaire got in before me so disregard the quoted response.


As an Irishman, I am not familiar with this "extra credit" concept.. I also wasn't aware it was school work, otherwise I wouldn't have posted a solution. Based on what I have just learned about extra credit, it would be wrong to provide you with solutions as in my eyes that is cheating.. Extra Credit should be awarded for a person's ability to use what they have been taught and build on it in order to complete the assignment so that they are honestly rewarded for an honest achievement.

You are supposed to generate a random number.. Where is your rand.next() code from your first post? Was that your code or someone else's? Have you dealt with random numbers in class yet? if not, google "vb.net generate random number"

You are supposed to display a message saying both "too low" AND "too high" if the guess is too low or too high but your code only states if the guess number is not equal to 7 then show a message saying it is too high.. your code should include handling the possibility of a guess number greater than, and a guess number less than, the "random" number. (general mathematical symbols apply). Again your first post made an attempt to handle this but now not?
 
Last edited:
22-degrees:

I edited my post one minute after you posted. Please check for updates.

Note: Any number type can be used with TryParse. Example:

Dim mynum As Double, snum As String
snum = 456.78
Double.TryParse(snum, mynum)
 
Back
Top