473,434 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

how do i make sure num1 is larger then num2

Bob
ok i have a program tah randomizes number adn signs (+,-). i made it
so little kids can learn math easily and i wouldn't have to write each
question out.

when the plus sign is used, eveyrthing works fine because they just
add the two numbers. when the computer chooses the minus sign,
sometimes the first number is smaller then the second and the kids
cant do it because they do not know negative numbers.

so my question is, how do i make sure that the first number (num1) is
always bigger then (num2)?

i have a "next" button to ask for the next question, when the user
presses it, the computer tells the user if its right or wrong and
gives the next question.

here is my code:

Dim num1 As Integer 'first number
Dim num2 As Integer 'second number
Dim sign(2) As String 'the sign that will be used
Private Sub Form_Load()

sign(0) = "+"
sign(1) = "-"

Randomize
'randomize the computer
lblNum1.Caption = Int(20 * Rnd)
num1 = lblNum1.Caption
'randomize num1 and store value
lblNum2.Caption = Int(20 * Rnd)
num2 = lblNum2.Caption
'randomize num2 and store value
lblSign.Caption = sign(Int(2 * Rnd))
'randomize sign

End Sub

Private Sub cmdNext_Click()

If lblSign.Caption = "+" Then
If Val(txtAnswer.Text) = Val(num1 + num2) Then
MsgBox ("Great Job")
ElseIf Val(txtAnswer.Text) <> Val(num1 + num2) Then
MsgBox "Look at the numbers carefully next time!",
vbExclamation
End If
ElseIf lblSign.Caption = "-" Then
If Val(txtAnswer.Text) = Val(num1 - num2) Then
MsgBox ("Great Job")
ElseIf Val(txtAnswer.Text) <> Val(num1 + num2) Then
MsgBox "Look at the numbers carefully next time!",
vbExclamation
End If
End If

Randomize
'randomize the computer
lblNum1.Caption = Int(20 * Rnd)
num1 = lblNum1.Caption
'randomize num1 and store value
lblNum2.Caption = Int(20 * Rnd)
num2 = lblNum2.Caption
'randomize num2 and srote value
lblSign.Caption = sign(Int(2 * Rnd))
'randomize sign

txtAnswer.Text = ""

End Sub
Jul 17 '05 #1
7 2160
Two quick points. First, only execute the Randomize statement **once**
within your program (in the Form_Load event is fine). Although it won't
matter for your present application, overuse of Randomize actually reduces
the range of random numbers that are returned. Second, you have an unusual
style of commenting... you are probably the only one I've ever seen who
places the comment **after** the statement it applies to as opposed to
before it.

See my comment at the end of this section of your code....
Private Sub Form_Load()

sign(0) = "+"
sign(1) = "-"

Randomize
'randomize the computer ****************************************** lblNum1.Caption = Int(20 * Rnd)
num1 = lblNum1.Caption
'randomize num1 and store value
lblNum2.Caption = Int(20 * Rnd)
num2 = lblNum2.Caption
'randomize num2 and store value
lblSign.Caption = sign(Int(2 * Rnd))
'randomize sign ******************************************
End Sub


I'm going to suggest that you do this last part (between the lines of
asterisks that I've added) of this just a little bit different that you
presently are in order to pick up the flexibility you require. Instead of
assigning you random value to the Label's Captions first and then using the
Captions to assign the value to your num1 and num2 variables, I going to
reverse that order and delay assigning the values to the Captions until
after we check if the first number will be smaller than the second one. If
it is smaller, we will reverse them. Try replacing the section I've outlined
with asterisks with this (I'm using the same commenting style you presently
use):

num1 = Int(20 * Rnd)
' Randomize num1 and store value
num2 = Int(20 * Rnd)
' Randomize num2 and srote value
If num1 < num2 Then
Temp = num1
num1 = num2
num2 = Temp
End If
' The above If-Then block swaps the values in
' the two variables if num1 is less than num2

lblNum1.Caption = num1

' Assign num1 to the Caption of lblNum1
lblNum2.Caption = num2
' Assign num2 to the Caption of lblNum2
lblSign.Caption = sign(Int(2 * Rnd))
' Randomize sign

Of course, you will have to make this same change at the end of your
cmdNext_Click code module also, given the way you structured your program.
For future consideration, you might want to check out how Functions and Sub
(also know as subroutines) work. They allow you to have one piece of code
that can be called from multiple locations within your program without
having to repeat all of the code over and over again like you did with the
code in the above section..

Rick - MVP
Jul 17 '05 #2
"Bob" <co*******@hotmail.com> wrote in message
so my question is, how do i make sure that the first number (num1) is
always bigger then (num2)?

Here is one way....

HTH
LFS

Option Explicit

Private Answer As Long

Private Sub Form_Load()
Randomize
Show
cmdNext = True
End Sub

Private Sub cmdGuess_Click()
If Val(txtAnswer) = Answer Then
MsgBox "Correct"
Else
MsgBox "The correct answer was: " & CStr(Answer)
End If
cmdNext = True
End Sub

Private Sub cmdNext_Click()
Dim num1, num2

txtAnswer = ""
If Rnd < 0.5 Then
lblSign = "+"
num1 = Int(Rnd * 20) + 1
num2 = Int(Rnd * 20) + 1
Answer = num1 + num2
Else
lblSign = "-"
num1 = Int(Rnd * 15) + 5
num2 = Int(Rnd * num1) + 1
Answer = num1 - num2
End If
lblNum1 = num1
lblNum2 = num2
If txtAnswer.Visible Then txtAnswer.SetFocus
End Sub

Private Sub txtAnswer_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
KeyCode = 0
cmdGuess = True
End If
End Sub


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 17 '05 #3
Bob
"Larry Serflaten" <Ab***@SpamBusters.com> wrote in message news:<40********@corp.newsgroups.com>...
"Bob" <co*******@hotmail.com> wrote in message
so my question is, how do i make sure that the first number (num1) is
always bigger then (num2)?

Here is one way....

HTH
LFS

Option Explicit

Private Answer As Long

Private Sub Form_Load()
Randomize
Show
cmdNext = True
End Sub

Private Sub cmdGuess_Click()
If Val(txtAnswer) = Answer Then
MsgBox "Correct"
Else
MsgBox "The correct answer was: " & CStr(Answer)
End If
cmdNext = True
End Sub

Private Sub cmdNext_Click()
Dim num1, num2

txtAnswer = ""
If Rnd < 0.5 Then
lblSign = "+"
num1 = Int(Rnd * 20) + 1
num2 = Int(Rnd * 20) + 1
Answer = num1 + num2
Else
lblSign = "-"
num1 = Int(Rnd * 15) + 5
num2 = Int(Rnd * num1) + 1
Answer = num1 - num2
End If
lblNum1 = num1
lblNum2 = num2
If txtAnswer.Visible Then txtAnswer.SetFocus
End Sub

Private Sub txtAnswer_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
KeyCode = 0
cmdGuess = True
End If
End Sub


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----


can u explain me this code, cause i got no idea wut ur doing here adn
y do u have option explicit at the top?

ur program looks cool and efficient so plz someone explain wut every
line does.
Jul 17 '05 #4
"Bob" <co*******@hotmail.com> wrote

Here is one way....
can u explain me this code, cause i got no idea wut ur doing here adn
y do u have option explicit at the top?

ur program looks cool and efficient so plz someone explain wut every
line does.


If you see a command you don't understand, put the cursor on it and
press F1 to bring up Help. Then read about it in Help, check out the
examples, etc. There is nothing there that is complicated. The one
command you may have difficulty with is:

cmdNext = True

Just think of it as the program's way of clicking on that button.
You can replace it with:

cmdNext_Click

Which calls the click routine (as if it was just a regular routine)
It would still work the same, try it and see....

LFS


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 17 '05 #5
> > can u explain me this code, cause i got no idea wut ur doing here adn
y do u have option explicit at the top?

ur program looks cool and efficient so plz someone explain wut every
line does.


If you see a command you don't understand, put the cursor on it and
press F1 to bring up Help. Then read about it in Help, check out the
examples, etc. There is nothing there that is complicated. The one
command you may have difficulty with is:

cmdNext = True

Just think of it as the program's way of clicking on that button.
You can replace it with:

cmdNext_Click

Which calls the click routine (as if it was just a regular routine)
It would still work the same, try it and see....


Actually, I think you have done something in your coding which may have also
confused Bob... you relied on the default properties of the controls being
used. For Bob's benefit, here is your code with those properties explicitly
included...

Rick - MVP

Option Explicit

Private Answer As Long

Private Sub Form_Load()
Randomize
Show
cmdNext.Value = True
End Sub

Private Sub cmdGuess_Click()
If Val(txtAnswer.Text) = Answer Then
MsgBox "Correct"
Else
MsgBox "The correct answer was: " & CStr(Answer)
End If
cmdNext.Value = True
End Sub

Private Sub cmdNext_Click()
Dim num1, num2
txtAnswer.Text = ""
If Rnd < 0.5 Then
lblSign.Caption = "+"
num1 = Int(Rnd * 20) + 1
num2 = Int(Rnd * 20) + 1
Answer = num1 + num2
Else
lblSign.Caption = "-"
num1 = Int(Rnd * 15) + 5
num2 = Int(Rnd * num1) + 1
Answer = num1 - num2
End If
lblNum1.Caption = num1
lblNum2.Caption = num2
If txtAnswer.Visible Then txtAnswer.SetFocus
End Sub

Private Sub txtAnswer_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
KeyCode = 0
cmdGuess.Value = True
End If
End Sub
Jul 17 '05 #6
"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote
Actually, I think you have done something in your coding which may have also
confused Bob...


You may be right, I just had a look at his first post and saw he qualified everything.
I guess thats one of those cases where RAD does not necessarily mean good!

Sorry for the confusion!
LFS

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 17 '05 #7
Bob
thanks a lot guys, i understand it all better now.
Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: msnews.microsoft.com | last post by:
Hi, I hope we can upload only files with limited size through ASP scripts(<2MB). I need to write an ASP script that can serve up to 100MB of file uploading. I have written one script(with...
4
by: Eyal Goren | last post by:
Hi, We have troubles when we try to use the 'dbuse' calls with databases larger than 28 characters, looks like the dbuse truncates the name after it. Any ideas ???
7
by: gino | last post by:
Dear all, My monitor was set to 1600x1200 so the fonts in IE is too small for me even when I set the "View->Text Size->Largest"... I don't have previlage to change the monitor resolution... ...
3
by: Netter | last post by:
I made a usercontrol (a couple of years ago). Actually it is a calculator with lots of little buttons. I have the control on a form. When I view the form in the IDE (or when running) the...
6
by: cooldisk | last post by:
Is it possible at all to read a binary file larger than 2GB on a 32- bit system? I tried the following: #include <iostream> #include <fstream> using namespace std; int main(int argc, char...
32
by: rfox | last post by:
When viewed in several different browsers, both Mac and PC, my fonts are consistent in size -- EXCEPT when viewed in IE7. In IE7, the fonts appear larger. Is there a way to ensure consistency in...
8
by: Avi | last post by:
Hi all, I'm using string Replace(string oldValue, string newValue) and would like it to replace only full words that matches oldValue and not when oldValue is a substring of a larger word. ...
19
by: Robert Kochem | last post by:
Hi, I am relative new to C++ regarding it's functions and libraries. I need to access files larger than 4GB which is AFAIK not possible with the STL iostream - at least not if using a 32 bit...
0
by: ll | last post by:
I'm working with 'pure ASP upload' script which is designed to redirect to an alert/error message, should a file larger than the set limit be attempted to be uploaded. The problem is that, while...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.