• YOU can help the next generation of students in the community!
    Share your trial papers and notes on our Notes & Resources page

need help with VB coding for major (1 Viewer)

Silverwolf

New Member
Joined
May 10, 2004
Messages
7
Location
Tea Gardens NSW
This is what I have;

If optDive = True Then
lblNum1.Caption = Empty
lblNum2.Caption = Empty
lblNum1.Caption = Int((100 - 1) * Rnd + 1)
lblNum2.Caption = Int((10 - 1) * Rnd + 1)
End If

I need the program to check if Num1 / Num2 gives an answer that includes decimals, and if so, to loop the above until such a time as the answer will be an integer. Any ideas? (tried a few different methods, but can't seem to come up with the appropriate coding)
 

J0n

N/A
Joined
Aug 28, 2003
Messages
410
Gender
Male
HSC
2004
You want to see if Num1 is divisible by Num2? If so, use mod - which gives the reamainder. So Num1 is divisible by Num2 if Num1 mod Num2 = 0. Another way is to check if Num1/Num2 = CInt(Num1/Num2). So for the loop, you can have something like:
While Num1 mod Num2 <> 0
...
Wend
 

Silverwolf

New Member
Joined
May 10, 2004
Messages
7
Location
Tea Gardens NSW
still not working, now it reckons I'm trying to divide by 0, (run-time error '11' - Division by zero)

thanx for trying though :)

ok, tryed this

Private Sub cmdNext_Click()
cmdNext.Caption = "&Next Question"
If optDive = True Then
lblNum1.Caption = Empty
lblNum2.Caption = Empty
lblNum1.Caption = Int((100 - 1) * Rnd + 1)
lblNum2.Caption = Int((10 - 1) * Rnd + 1)
While Num01 / Num02 = CInt(Num01 / Num02)
Wend
End If

Now I'm getting an overflow error

my Declerations are as follows

Option Explicit
Dim Num1 As Integer
Dim Num2 As Integer
Dim Ans As Integer
Dim Reply As Integer
Dim Num01 As Integer
Dim Num02 As Integer
Dim Message As String

tryed changing relevant ones to

Dim Num1 As Long
Dim Num2 As Long
Dim Num01 As Long
Dim Num02 As Long
&
Dim Num1 As Double
Dim Num2 As Double
Dim Num01 As Double
Dim Num02 As Double

Still got an overflow error, any ideas?
 
Last edited:

J0n

N/A
Joined
Aug 28, 2003
Messages
410
Gender
Male
HSC
2004
You are getting Division by Zero because the value of Num02 is 0 and a number divided by 0 is undefined.
You will probably have to check if Num02 is zero seperately.
e.g.
Code:
Public Function Divisible(Num1 As Integer, Num2 As Integer)
Divisible = IIf(Num2 = 0, True, False)
If Not (Num2 = 0) Then
     Divisible = IIf(Num1 mod Num2 = 0, True, False)
EndIf
End Function
Then you loop can just be:
Code:
While Divisible(Num01, Num02)
If optDive = True Then
     lblNum1.Caption = Empty
     lblNum2.Caption = Empty
     lblNum1.Caption = Int((100 - 1) * Rnd + 1)
     lblNum2.Caption = Int((10 - 1) * Rnd + 1)
End If
Wend
But of course, that will be an infinite loop since Num01 and Num02 are not being updated, did you mean lblNum2?
Btw, what to you count division by zero as - divisible or not divisible? I assumed divisible - if not just interchange True and False in the Decimal function.
 
Last edited:
Joined
May 27, 2004
Messages
107
Gender
Male
HSC
2004
in ur orginal thing u posted the second time there is nothing done in the while loop and it will keep going cause nothing changes

also what is optDive?

and you should see if there is anything in the lbl before doing the rest of the coding
 

Winston

Active Member
Joined
Aug 30, 2002
Messages
6,128
Gender
Undisclosed
HSC
2003
Originally posted by dastonecutters
in ur orginal thing u posted the second time there is nothing done in the while loop and it will keep going cause nothing changes

also what is optDive?

and you should see if there is anything in the lbl before doing the rest of the coding
I presume optDive is either of boolean type or it's a radio button or a checkbox control.
 

Silverwolf

New Member
Joined
May 10, 2004
Messages
7
Location
Tea Gardens NSW
Originally posted by dastonecutters
...also what is optDive?...
optDive stands for for Option Button / Division / Easy

ie. if optDive = true then the questions asked are easy (dividing # 1-9 into # 1-99)
whereas if optDivm = true then the questions asked are of medium difficulty (dealing with # 99-99). optDivh... well u get the picture :)

Originally posted by dastonecutters
...you should see if there is anything in the lbl before doing the rest of the coding...
the lines;
lblNum1.Caption = Empty
lblNum2.Caption = Empty
are supposed to remove anything thats already in the lbl
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top