Solved

Using comparison operators on more than one variable

Posted on 2003-10-21
12
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

752 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