Jump to content
New account registrations are disabed. This website is now an archive. Read more here.
albertibay

Visual Basic Help

Recommended Posts

um here is the whole script

i just don't understand whats wrong 1+1+1+1 divided by 4. VB Keeps on insisting its 3.25

vb-23878.png

Even with a direct approach it fails

vb2-23878.png

Edited by albertibay

Share this post


Link to post
Share on other sites

what you need to do is bracket your equation like so:

(1 + 1 + 1 + 1) / 4

as when you don't bracket it it adds the values together plus the last value divided by 4, so your essentially doing this:

1 + 1 + 1 + 0.25

but if you bracket it, what's inside the brackets is one value, in this case 4, then you divide that calculated value by 4 returning 1,

just remember that brackets are very important when calculating in programming, as the program takes the calculation very literal, also separating your equation into brackets even when not needed sometimes makes it easier to understand what's going on

Share this post


Link to post
Share on other sites

this is the same problem people have with math regularly. You have to remember that Division and Multiplication comes before Addition and Subtraction so if you don't use brackets it does them first. Computer programs do that literally like Diag said.

Share this post


Link to post
Share on other sites

Order of operations is very important when dealing with something like this. Brackets work in this case though it's not the best programming practice in my opinion. I'd say the total from the 4 text boxes should be stored into a local variable named something like, gradeTotal, and then use that variable to find the average, this has 2 benefits, the first being that it's easier to keep from making a simple mistake like that, the other is if you wanted to do more with the total it would much simpler to work with 1 variable than 4 values each time you wanted to make a call to the total.

Just as an example if you were doing a receipt program that took a list of values then found the subtotal, the sales tax, the total calculated change. Since the total is really being used once here though it really is moot point which method is used in this case though expanding on the program would warrant the necessity of variables

 

On a kinda off topic note, I'm surprised Basic is still being used. I guess it isn't all that unbelievable since it hasn't been that long since I last used basic, only 15 years ago.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...