calculation question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

From the looks of the post this is going to be a remedial question. I am
currently working on my CIS degree and I am in VB.net this semester, so I am
extremely new to all this. I'm trying create a program that will allow me to
average 5 numbers. Right now my calculation code reads as follows

Private Sub btncalculate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btncalculate.Click
Label7.Text = Val(txtscore1.Text) + (txtscore2.Text) _
+ (txtscore3.Text) + (txtscore4.Text) _
+ (txtscore5.Text) \ 5

However it will come out with the wrong average. If I take out the \ 5 ; it
will add everything perfect, so my question is how do I get it to divide. Any
help will be appreciated.

Thanks,
Ryan
 
ryan said:
From the looks of the post this is going to be a remedial question. I am
currently working on my CIS degree and I am in VB.net this semester, so I
am
extremely new to all this. I'm trying create a program that will allow me
to
average 5 numbers. Right now my calculation code reads as follows

Private Sub btncalculate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btncalculate.Click
Label7.Text = Val(txtscore1.Text) + (txtscore2.Text) _
+ (txtscore3.Text) + (txtscore4.Text) _
+ (txtscore5.Text) \ 5

However it will come out with the wrong average. If I take out the \ 5 ;
it
will add everything perfect, so my question is how do I get it to divide.
Any
help will be appreciated.

Thanks,
Ryan

ryan,

What data type are the numbers? Double, Int??? In the case of double, just
replace Integer with Double :)

Label7.Text = (Integer.Parse(txtscore1.Text) + Integer.Parse(txtscore2.Text)
+ _
Integer.Parse(txtscore3.Text) + Integer.Parse(txtscore4.Text) + _
Integer.Parse(txtscore5.Text)) / 5

Basically, the order in which the compiler calculates this expression is
different than how I believe you think it is. The compiler, just as it
should, doesn't add all of the numbers together then divides...the division
operator is of higher precedence than the addition operator, therefore it
does txtscore5.Text \ 5 before the additions of the numbers.

By wrapping all of the additions together using parenthesis, we are forcing
the compiler to calculate the expression(s) inside the parenthesis prior to
calculating the expression(s) outside the parenthesis.

Integer.Parse is a more .Net'ish way of converting a string to an integer :)

Hope this helps :)

Mythran
 
Try adding a couple of more parenthese:

Label7.Text = (Val(txtscore1.Text) + (txtscore2.Text) _
+ (txtscore3.Text) + (txtscore4.Text) _
+ (txtscore5.Text)) \ 5
 
ryan said:
Private Sub btncalculate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btncalculate.Click
Label7.Text = Val(txtscore1.Text) + (txtscore2.Text) _
+ (txtscore3.Text) + (txtscore4.Text) _
+ (txtscore5.Text) \ 5

However it will come out with the wrong average. If I take out the \ 5 ; it
will add everything perfect, so my question is how do I get it to divide. Any
help will be appreciated.


Operator precedence decides which operations will be performed first. In
the above expression (txtscore5.text) \ 5 will be the first thing done because
\ has a higher priority than all those + operators. The parentheses allow you
to declare the evaluation order you want. See Precedence (operator) in Help.

I would also suggest you shouldn't be trying to add strings. Consider:

answer = "one" + "two"

You know you could never get "three" in the answer, but that is very similar
to what you are expecting from the addition of all those strings. It would be
more proper to convert all the input to appropreate data types and then perform
the calculation.

LFS
 
Back
Top