Tuesday, July 14, 2009

Visual Basic 6.0 help?

Okay I've been trying to get this to work for quite a while now. I'm supposed to make a program that simulates a guessing game where someone tries to guess a number between 1 and 100. It also should tell the person whether they're to high/low etc. and keep a running total of guesses taken.





The only problem I'm having is keeping the random number. Every time I hit my "guess" button it tells me if i'm right or wrong but then when I hit it again it generates a new random number. How do I keep that random number so I can keep guessing until it's right?





Private Sub cmdGuess_Click()





c = c + 1


n = Int(Rnd * 101) + 1


lblNumber.Caption = "Number of guesses:" + LTrim$(Str$(c))


If n %26lt; txtGuess.Text Then


lblGuess.Caption = "Your guess is too HIGH. Try Again."


ElseIf n %26gt; txtGuess.Text Then


lblGuess.Caption = "Your guess is too LOW. Try Again."


ElseIf n = txtGuess.Text Then


lblGuess.Caption = "Congradulations you are CORRECT!"


End If

Visual Basic 6.0 help?
You need to create a sub procedure for the random number generator, and call it only after the user either gets it right, or after the 10th wrong guess. You can't put the random number generator in the click event, otherwise everytime you click it, the number will regenerate. I'm actually working on this same problem, and on my other computer I have a link to another messageboard where I posted my exact code and got help from people. Mine works great now! If I remember, I will post the link to my code.
Reply:define the number and assign a value for it in the general segment ... so that you won't be reassigning in this command


No comments:

Post a Comment