Solved

Using comparison operators on more than one variable

Posted on 2003-10-21
12
195 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now