473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

calculation question

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

Nov 21 '05 #1
3 1057

"ryan" <ry**@discussions.microsoft.com> wrote in message
news:93**********************************@microsof t.com...
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
Nov 21 '05 #2
Try adding a couple of more parenthese:

Label7.Text = (Val(txtscore1.Text) + (txtscore2.Text) _
+ (txtscore3.Text) + (txtscore4.Text) _
+ (txtscore5.Text)) \ 5
"ryan" wrote:
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

Nov 21 '05 #3

"ryan" <ry**@discussions.microsoft.com> wrote
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
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: rodger | last post by:
hi all, I have a form with two text boxes, a go button and a formula in the code to perform a calculation a value is entered in one text box, and press buttonm a result is computed which is...
0
by: gavo | last post by:
Hi. using A2K; i have a form containing a continous subform. The question is, how can i call a calculation for one of the fields in the continous subform from the main form. At the moment i...
4
by: Michiel Alsters | last post by:
Hello everybody, I hope anybody can help me. I'll try to give a brief overview of my problem. I have running a program that performs a heavy calculation. To give the user feedback what the...
3
by: linq936 | last post by:
I have some algorithm dealing with unsigned int calculation a lot, I am trying to add some check for overflow. The initial thinking was very easy, just do something like this, int...
6
by: David | last post by:
Hi, I have the following calculation I am trying to work out ..... Not sure how ? Variable 1 = minstock Variable 2 = oDict(oKey) I need to test if my variable 'minstock' <= 40% of...
1
by: gjoneshtfc | last post by:
Hello, I have an asp page that is a table. In the table the end column is a calculation of other values, for example: ...
28
by: beach.dk | last post by:
Hi, I'm trying to implement a simple hash algorith called rs_hash in javascript, but I cannot get a correct result. In c the code looks like this:
5
by: The alMIGHTY N | last post by:
Hi all, Let's say I have a simple math formula: sum (x * y / 1000) / (sum z / 1000) I have to do this across 50 items, each with an x, y and z value, when the page first loads AND when a...
3
by: mattmao | last post by:
Okay, I was asked by a friend about the result of this limit: http://bbs.newwise.com/attdata/forumid_14/20070922_fe7f77c81050413a20fbDWYOGm7zeRj3.jpg Not n->zero but n-> + infinite I really...
1
by: csolomon | last post by:
I have an unbound text box which I use to get a calculation, called DM_SampleWt. This text box's control source is: =GetSize(,Nz(,0),.!,.!.(2)) I have written a function that will get the required...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.