2 Replies - 1029 Views - Last Post: 17 January 2009 - 02:50 PM Rate Topic: -----

#1 warxc++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-July 08

Guessing game problem

Post icon  Posted 16 January 2009 - 03:36 PM

i am making a guessing game where the limit is 4 guesses ,where the target is a random number between 1 and 7, and if the limit is reached the program exits. Also a message box displays if u won, lost or if u got it wrong. My problem is that the message box (incorrect guess one) keeps popping up and doesn't allow for input for another guess. Then the message box(game over one) displays and the program exits.

need help plz here is code

Public Class Form1

	Dim intsecret As Integer
	Dim intguess As Integer

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

		Dim counter As Integer

		Do
			intguess = Val(Me.TextBox1.Text)
			If intguess <> intsecret Then
				MessageBox.Show("Incorrect guess")
			End If
			counter += 1
			If intguess = intsecret Then
				MessageBox.Show("You Won")
			End If
			If counter = 4 Then
				MessageBox.Show("Game Over")
				Application.Exit()
			End If
		Loop While intguess <> intsecret Or counter <> 4



	End Sub

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Randomize()

		intsecret = (7 * Rnd()) + 1
	End Sub
End Class


This post has been edited by warxc++: 16 January 2009 - 03:38 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Guessing game problem

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Guessing game problem

Posted 16 January 2009 - 03:58 PM

The problem is because your loop never has to stop and wait for input from the user. By you taking the input from the textbox, the loop is going to keep executing using the value the user put in that box over and over again. So if the first time they put in a wrong guess, each time through the loop it continues to use that same value. Hopefully that makes sense to you. The program loop there doesn't have to stop it keeps reading the textbox uninterrupted.

What you need to do is design something which will reprompt the user for their next guess... that or maybe redesign this so that when they click a button it then checks the textbox ONCE, determines if it is wrong, increments the guess count and then stops... waiting for the user to enter another value into the text box and hit the button again.

:)
Was This Post Helpful? 0
  • +
  • -

#3 warxc++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-July 08

Re: Guessing game problem

Posted 17 January 2009 - 02:50 PM

oh ok thx for the advice. Now i know what im doing wrong. I was just wondering is there a way to make the program stop so the user can enter a number.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1