Solved

Using comparison operators on more than one variable

Posted on 2003-10-21
12
214 Views
Last Modified: 2010-05-03
I have a program in which I have 10 numbers, and am trying to see which one is the highest.  This will return odd results when I know for a fact that the variables are set correctly.

CODE:

If frmp1.p10balence > frmp1.p2balence And frmp1.p10balence > frmp1.p3balence And frmp1.p10balence > frmp1.p4balence And frmp1.p10balence > frmp1.p5balence And frmp1.p10balence > frmp1.p6balence And frmp1.p10balence > frmp1.p7balence And frmp1.p10balence > frmp1.p8balence Then MsgBox ("Player 1 Wins!")
If frmp1.p2balence > frmp1.p10balence And frmp1.p2balence > frmp1.p3balence And frmp1.p2balence > frmp1.p4balence And frmp1.p2balence > frmp1.p5balence And frmp1.p2balence > frmp1.p6balence And frmp1.p2balence > frmp1.p7balence And frmp1.p2balence > frmp1.p8balence Then MsgBox ("Player 2 Wins!")
If frmp1.p3balence > frmp1.p2balence And frmp1.p3balence > frmp1.p10balence And frmp1.p3balence > frmp1.p4balence And frmp1.p3balence > frmp1.p5balence And frmp1.p3balence > frmp1.p6balence And frmp1.p3balence > frmp1.p7balence And frmp1.p3balence > frmp1.p8balence Then MsgBox ("Player 3 Wins!")
If frmp1.p4balence > frmp1.p2balence And frmp1.p4balence > frmp1.p3balence And frmp1.p4balence > frmp1.p10balence And frmp1.p4balence > frmp1.p5balence And frmp1.p4balence > frmp1.p6balence And frmp1.p4balence > frmp1.p7balence And frmp1.p4balence > frmp1.p8balence Then MsgBox ("Player 4 Wins!")
If frmp1.p5balence > frmp1.p2balence And frmp1.p5balence > frmp1.p3balence And frmp1.p5balence > frmp1.p4balence And frmp1.p5balence > frmp1.p10balence And frmp1.p5balence > frmp1.p6balence And frmp1.p5balence > frmp1.p7balence And frmp1.p5balence > frmp1.p8balence Then MsgBox ("Player 5 Wins!")
If frmp1.p6balence > frmp1.p2balence And frmp1.p6balence > frmp1.p3balence And frmp1.p6balence > frmp1.p4balence And frmp1.p6balence > frmp1.p5balence And frmp1.p6balence > frmp1.p10balence And frmp1.p6balence > frmp1.p7balence And frmp1.p6balence > frmp1.p8balence Then MsgBox ("Player 6 Wins!")
If frmp1.p7balence > frmp1.p2balence And frmp1.p7balence > frmp1.p3balence And frmp1.p7balence > frmp1.p4balence And frmp1.p7balence > frmp1.p5balence And frmp1.p7balence > frmp1.p6balence And frmp1.p7balence > frmp1.p10balence And frmp1.p7balence > frmp1.p8balence Then MsgBox ("Player 7 Wins!")
If frmp1.p8balence > frmp1.p2balence And frmp1.p8balence > frmp1.p3balence And frmp1.p8balence > frmp1.p4balence And frmp1.p8balence > frmp1.p5balence And frmp1.p8balence > frmp1.p6balence And frmp1.p8balence > frmp1.p7balence And frmp1.p8balence > frmp1.p10balence Then MsgBox ("Player 8 Wins!")
0
Comment
Question by:drtsystems
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9595771
Please maintain your open questions:

1 03/11/2003 60 Serial LCD  Unlocked Hardware
2 02/21/2003 50 Windows RARP Server  Unlocked Microsoft Network

Thanks,
Anthony
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 9596088
You need to put () around the compares.  AND is higher precedence so it executes first.

If (frmp1.p10balence > frmp1.p2balence) And (frmp1.p10balence > frmp1.p3balence) And

ETC

mlmcc
0
 
LVL 5

Accepted Solution

by:
fantasy1001 earned 50 total points
ID: 9597684
Make is this way--> bubble sort

dim myarray(9) as integer
myarray(0) = frmp1.p1balence
myarray(1) = frmp1.p2balence
myarray(2) = frmp1.p3balence
myarray(3) = frmp1.p4balence
myarray(4) = frmp1.p5balence
myarray(5) = frmp1.p6balence
myarray(6) = frmp1.p7balence
myarray(7) = frmp1.p8balence
myarray(8) = frmp1.p9balence  
myarray(9) = frmp1.p10balence

Do
    AnyChanges = False
    For i = 1 To UBound(myarray()) - 1
         If myarray(i) > myarray(i + 1) Then
              ' These two need to be swapped
              SwapFH = myarray(i)
              myarray(i) = myarray(i + 1)
              myarray(i + 1) = SwapFH
              AnyChanges = True
         End If
    Next i
Loop Until Not AnyChanges

Thanks & Cheers
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 100

Expert Comment

by:mlmcc
ID: 9603570
This probably a homework problem and they haven't gotten to arrays just yet.

mlmcc
0
 

Author Comment

by:drtsystems
ID: 9603796
actually, this isnt a homework project, it is a little "game" i started a while back, when i didnt know too much about programming.  I now program primarily in C++, and have trouble with VB, considering C++ is a "stronger-typed language" (or whatever it called, having to difine variables more explicitly) and I always end up screwing up my program with anything more than a simple variable.
0
 

Author Comment

by:drtsystems
ID: 9603810
Sorry for the double post, I forgot to add this.  I will give credit to someone when I am able to test it.  I know fantasy1001 is probably better, but mlmcc has a more quick fix it seems, as I dont feel like rewriting a whole section of code.
0
 
LVL 5

Expert Comment

by:fantasy1001
ID: 9603833
There is not need to write all the code again. You can just copy the code I gave in replace with your original, and add this line

msgbox "Player " & myarray(9) & " wins"

Anyway, the code also show the bubble sort technique which is popular in c++.

thanks & cheers
0
 

Author Comment

by:drtsystems
ID: 9610801
using fantasy's way, i end up with "Player 0 wins" when player 2 should have won.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9610942
No telling, but I suspect this line:
For i = 1 To UBound(myarray()) - 1

is supposed to be:
For i = 0 To UBound(myarray()) - 1

Anthony

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9610954
Also, let me know if you need help maintaining your open questions:

Question Title: Serial LCD
Author: drtsystems
Points: 60
Date: 03/11/2003 08:02PM CST
http://www.experts-exchange.com/Hardware/Q_20547165.html

Question Title: Windows RARP Server
Author: drtsystems
Points: 50
Date: 02/21/2003 05:45PM CST
http://www.experts-exchange.com/Networking/Microsoft_Network/Q_20524577.html

Thanks,
Anthony
0
 

Author Comment

by:drtsystems
ID: 9610995
Fixed.  The only problem is that this displayes the value of the variables.  Lets say p2 balence is higher, i want "Player 2 Wins" not "Player 777 Wins" beucase player 2 had 777

Thanks!
0
 
LVL 5

Expert Comment

by:fantasy1001
ID: 9624528
Sorry for the confusion, add a integer variable name UserWon

         If myarray(i) > myarray(i + 1) Then
              ' These two need to be swapped
              SwapFH = myarray(i)
              myarray(i) = myarray(i + 1)
              myarray(i + 1) = SwapFH
              AnyChanges = True

              UserWon = i + 1       'Add here
         End If

Thanks & cheers
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question