Lottery Program Help

Page 1 of 1

9 Replies - 6460 Views - Last Post: 06 December 2010 - 07:42 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=203769&amp;s=4b33773cea1cf9b22f586b6bdf3dbc0d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 zany315

Reputation: 0
• Posts: 5
• Joined: 05-December 10

Lottery Program Help

Posted 05 December 2010 - 10:25 AM

Hello I am a new programmer in visual basics. Right now I am stomped with this recent assignment.

Write a program to play a lottery game. The player will select 3 numbers between 1 and 9; these
numbers do not have to be unique. For example, the player may select 3, 2, 9 or 6, 2, 2. The winning
numbers are randomly generated by the computer. If all three of the numbers selected by the player are
the same as the winning numbers generated, then the player wins, otherwise the player loses. The
player’s numbers and the winning numbers must be an exact match (the order the numbers appear must
match).

And so far I have:
```Dim num1 As Integer
Dim num2 As Integer
Dim num3 As Integer

num1 = txtnum1.Text
num2 = txtnum2.Text
num3 = txtnum3.Text
num1 = CInt(txtnum2.Text)
num2 = CInt(txtnum2.Text)
num3 = CInt(txtnum3.Text)

Dim randomNum As New Random 'creates a random number generator
Dim winningNum1 As Integer
winningNum1 = randomNum.Next(1, 9)
Dim winningNum2 As Integer
winningNum2 = randomNum.Next(1, 9)
Dim WinningNum3 As Integer
WinningNum3 = randomNum.Next(1, 9)
```

I am having difficulties converting the numbers to string. Also making a textbox visible after the button has been clicked. Please help

This post has been edited by modi123_1: 05 December 2010 - 11:45 AM
Reason for edit:: please use the code tags..

Is This A Good Question/Topic? 0

Reputation:

Re: Lottery Program Help

Posted 05 December 2010 - 02:56 PM

for converting a integer to string use: intname.ToString
for making a textbox visible use : "textbox's name".visible = True

and excuse me for the message without any content

#3 CruNcK

Reputation: 0
• Posts: 2
• Joined: 05-December 10

Re: Lottery Program Help

Posted 05 December 2010 - 02:59 PM

I also have another remark, if you generate a number with a random, and you typ (1,9), it'll generate numbers from 1 to 8, because it is exclusive upperbount

#4 zany315

Reputation: 0
• Posts: 5
• Joined: 05-December 10

Re: Lottery Program Help

Posted 05 December 2010 - 03:56 PM

Thanks.

winningNum1.visible = True It says this is not integer. How would I convert this to an integer. I am really having trouble converting may someone explain that to me?

#5 zany315

Reputation: 0
• Posts: 5
• Joined: 05-December 10

Re: Lottery Program Help

Posted 05 December 2010 - 04:06 PM

The latest code:
```  'Written by Gerardo Morillo
Dim num1 As Integer
Dim num2 As Integer
Dim num3 As Integer

num1 = txtnum1.Text
num2 = txtnum2.Text
num3 = txtnum3.Text
num1 = CInt(txtnum2.Text)
num2 = CInt(txtnum2.Text)
num3 = CInt(txtnum3.Text)
num1.ToString()
num2.ToString()

Dim randomNum As New Random 'creates a random number generator
Dim winningNum1 As Integer
winningNum1 = randomNum.Next(1, 10)
Dim winningNum2 As Integer
winningNum2 = randomNum.Next(1, 10)
Dim WinningNum3 As Integer
WinningNum3 = randomNum.Next(1, 10)

winningNum1.visible = True
winningNum1.ToString(winningNum1)

If winningNum1 = num1 Then
num1 = True

```

And from here I am totally lost!

MOD EDIT: When posting code...USE CODE TAGS!!!

This post has been edited by JackOfAllTrades: 06 December 2010 - 09:01 AM

#6 CharlieMay

• This space intentionally left blank

Reputation: 1696
• Posts: 5,529
• Joined: 25-September 09

Re: Lottery Program Help

Posted 06 December 2010 - 06:37 AM

This is unnecessary
```num1 = txtnum1.Text
num2 = txtnum2.Text
num3 = txtnum3.Text
```

It may not throw an error with Option Strict Off. It will definitely throw an error with Option Strict On as you are trying to make an integer = a string and you can only do this if the string can be converted to an integer.

This is ok
```num1 = CInt(txtnum2.Text)
num2 = CInt(txtnum2.Text)
num3 = CInt(txtnum3.Text)
```

But you're making a huge assumption that the user is going to follow instruction and enter a number in the textbox that can be cast to an integer. Either use a numericupdown control to force the user to only use a number or use .TryParse to test the textboxes.

Not sure why you're doing this as it is doing nothing at all for you.
num1.ToString()
num2.ToString()

Try removing
```num1 = txtnum1.Text
num2 = txtnum2.Text
num3 = txtnum3.Text
```

and
```num1.ToString()
num2.ToString()
```

Now this:
```winningNum1.visible = True
winningNum1.ToString(winningNum1)
```
Will not work because an integer has no visible property. If you want to display the result of winningNum1 you could add a label and set its .Text property to winningNum1.Tostring()
This will show what the number generated from the random is.
The second line even if it was set up correctly would not do anything much like the num1.tostring() line above.
Remove both of thes lines.

The last thing you have is num1 = true. num1 is an integer and true is a boolean result. You need to use an if statement to check for true and then display when it is:

Example:
```If winningNum1 = num1 then
MessageBox.Show("You matched number 1 to a random number")
End If
```

Keep in mind, you will only see the messagebox IF the number typed into textbox1 matches the random number generated for winningNum1. So you would have to do this for each of the winningnum(s) and num(s) variables. Either by adding additional If...Then Blocks or using ElseIf for the other comparisons.

see if that gets you closer to your goal and then we can help you use methods appropriate to the class assignment.

This post has been edited by CharlieMay: 06 December 2010 - 06:58 AM

#7 zany315

Reputation: 0
• Posts: 5
• Joined: 05-December 10

Re: Lottery Program Help

Posted 06 December 2010 - 08:13 AM

I am still confused about the conversion process.

```Dim num1 As Integer = CStr(txtnum1.Text)
Dim num2 As Integer = CStr(txtnum2.Text)
Dim num3 As Integer = CStr(txtnum3.Text)
Dim messagebox As String
Dim winningNum1 As Integer
Dim winningnum2 As Integer
Dim winningnum3 As Integer

```

This part here is my biggest problem. I am not sure what to do here. How do you make it so that the conversion from int to string is acceptable?

MOD EDIT: When posting code...USE CODE TAGS!!!

This post has been edited by JackOfAllTrades: 06 December 2010 - 09:02 AM

#8 CruNcK

Reputation: 0
• Posts: 2
• Joined: 05-December 10

Re: Lottery Program Help

Posted 06 December 2010 - 09:44 AM

do it like this:

dim num1, num2, num3, winningNum1, winningNum2, winningNum3 as integer

num1 = integer.parse(txtnum1.text)
num2 = integer.parse(txtnum2.text)
num3 = integer.parse(txtnum3.text)

#9 begbodyguard

Reputation: 0
• Posts: 33
• Joined: 15-February 10

Re: Lottery Program Help

Posted 06 December 2010 - 02:04 PM

```'Declaration

'User numbers
Dim intnum1 As Integer = CInt(txtnum1.Text)
Dim intnum2 As Integer = CInt(txtnum2.Text)
Dim intnum3 As Integer = CInt(txtnum3.Text)

'Winning numbers (pc)
Dim intwinningNum1 As Integer
Dim intwinningnum2 As Integer
Dim intwinningnum3 As Integer

'Booleans to store if number1 = winningnumber and number2 .....
Dim bliscorrect1 as boolean
Dim bliscorrect2 as boolean
Dim bliscorrect3 as boolean
'Make the pc choose a random number
Dim randomx as new random

```

Changed it a little bit start too with

#10 zany315

Reputation: 0
• Posts: 5
• Joined: 05-December 10

Re: Lottery Program Help

Posted 06 December 2010 - 07:42 PM

Thanks all for the help. It really did help!