Link to home
Start Free TrialLog in
Avatar of drtsystems
drtsystems

asked on

Using comparison operators on more than one variable

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!")
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

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
Avatar of Mike McCracken
Mike McCracken

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
ASKER CERTIFIED SOLUTION
Avatar of fantasy1001
fantasy1001

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This probably a homework problem and they haven't gotten to arrays just yet.

mlmcc
Avatar of drtsystems

ASKER

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.
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.
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
using fantasy's way, i end up with "Player 0 wins" when player 2 should have won.
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

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
https://www.experts-exchange.com/questions/20547165/Serial-LCD.html

Question Title: Windows RARP Server
Author: drtsystems
Points: 50
Date: 02/21/2003 05:45PM CST
https://www.experts-exchange.com/questions/20524577/Windows-RARP-Server.html

Thanks,
Anthony
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!
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