?
Solved

A program that accepts the 5 grades given then average it.

Posted on 2003-03-03
7
Medium Priority
?
132 Views
Last Modified: 2010-05-01
I used the following declarations:

Dim num, ave
num = so and so....
num = so and so....

My problem is this:
num = g1 + g2 + g3 + g4 + g5
ave = num/5

If i have inputted 5 same grades equivalent to 1, the output of my program would be 2222.2 instead of 1. Again i f i have inputted 5 same grades equivalent to 2, the output of my program would be 4444.4

Please help me. Thanks
0
Comment
Question by:chatle
[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
7 Comments
 
LVL 3

Expert Comment

by:DeAn
ID: 8057798
is this what you mean?

    Dim num As Single, ave As Single
    Dim g1 As Single, g2 As Single, g3 As Single, g4 As Single, g5 As Single
    g1 = 2
    g2 = 2
    g3 = 2
    g4 = 2
    g5 = 2
   
    num = g1 + g2 + g3 + g4 + g5
    ave = (num / 5) * 2222.2
   
    MsgBox ave
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 400 total points
ID: 8057865
The problem is that you have declared the variables without type and they have therefore defaulted to variant. More specifically you have probably assigned the value of a textbox to each variable thus giving a subtype of string. Using the + operator on these will give you string concatenation rather than arithmetic addition. Your calculation is then correct as it is dividing 11111 by 5 to get 2222.2.

Better to ensure that you are always using numbers.

Dim num as Single
Dim g1 As Single
Dim g2 As Single
Dim g3 As Single
Dim g4 As Single
Dim g5 As Single
Dim ave As Single
g1 = CSng(Text1.Text)
g2 = CSng(Text2.Text)
g3 = CSng(Text3.Text)
g4 = CSng(Text4.Text)
g5 = CSng(Text5.Text)
num = g1 + g2 + g3 + g4 + g5
ave = num /5
Msgbox ave
0
 

Expert Comment

by:legendsfan
ID: 8057996
This sub will allow you to use as many or few grades as you would like.  Now you can figure grades halfway through the semester. Just add more textboxes as when more grades are required

Private Sub Command1_Click()
    Dim g1 As String
    Dim g2 As String
    Dim g3 As String
    Dim g4 As String
    Dim g5 As String
    Dim intNumGrades As Integer
    Dim ctl As Control
    Dim dblTotal As Double
   
    dblTotal = 0
    intNumGrades = 0
   
    g1 = Text1.Text
    g2 = Text2.Text
    g3 = Text3.Text
    g4 = Text4.Text
    g5 = Text5.Text
   
    For Each ctl In Me.Controls
        If TypeOf ctl Is TextBox And ctl <> "" Then
            dblTotal = dblTotal + CInt(ctl.Text)
            intNumGrades = intNumGrades + 1
        End If
    Next
    Label1.Caption = dblTotal / intNumGrades
   
End Sub
0
Independent Software Vendors: 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 101

Expert Comment

by:mlmcc
ID: 8058121
Be sure to give credit to you rexperts when you turn your homework in.

mlmcc
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8059300
Another piece of advice:

ALWAYS delcare the TYPE of ALL your variables.  It is a very bad habit to simply use

Dim MyVar

rather than

Dim MyVAr as _________

where the _________ is the name of a SPECIFIC TYPE, like Integer, or Single, or String, or Date.

AW

0
 

Author Comment

by:chatle
ID: 8151220
Dean,

For the effort you've done, i give you 10 points only. you answer is not actually what i meant.
Thanks again.

Chatle
0
 

Author Comment

by:chatle
ID: 8151238
Dear Tim,

Thanks for your answer. Sorry for the delayed of my rating.
You're such a wonderful programmer.


Regards,

Chatle
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month13 days, 3 hours left to enroll

777 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