mbt masai
 
Welcome !
         

                                
After experiencing a lot of down time, We decided to move this site to CrystalTech.com. CrystalTech.com is powered by only the finest Windows servers providing the best performance, reliability, and value anywhere.

 A simple calculator.

Author Message
logan

  • Total Posts : 2
  • Scores: 0
  • Reward points : 0
  • Joined: 8/19/2010
  • Status: offline
A simple calculator. Thursday, August 19, 2010 6:37 AM (permalink)
0
This is my first VBScript, so forgive me if it's badly coded. It's a simple to number calculator, with no error handling, as I don't know how to do that yet. :P
     dim d 
     dim boo 
     dim a 
     dim b 
     boo=inputbox("A for add, S for subtract, M for multiply, D for divide.") 
     function math(c) 
         a=inputbox("What is the number you want first in the equation?") 
         b=inputbox("What is the number you want second in the equation?") 
         if c = "a" then 
             d = a + b 
         elseif c = "s" then 
             d = a - b 
         elseif c = "d" then 
             d = a / b 
         elseif c = "m" then 
             d = a * b 
         end if 
     end function 
     math(boo) 
     if boo="a" then 
         msgbox("The sum is " & d & ".") 
     elseif boo="s" then 
         msgbox("The difference is " & d & ".") 
     elseif boo="m" then 
         msgbox("The product is " & d & ".") 
     elseif boo="d" then 
         msgbox("The quotient is " & d & ".") 
     end if

#1
    ebgreen

    • Total Posts : 8088
    • Scores: 95
    • Reward points : 0
    • Joined: 7/12/2005
    • Status: offline
    Re:A simple calculator. Thursday, August 19, 2010 8:21 AM (permalink)
    0
    Thanks for sharing. Doing little things like this is the best way to learn in my opinion. I will give you some ideas of habits that if you develop them as you learn will make your life much easier in the long run:

    • Option Explicit - Look up the documentation on this, but you really want to use it in every script that you write. It prevents simple typo errors that are often very hard to troubleshoot.
    • Variable Names - Use good expressive meaningful variable names. In a simple script like this, it isn't critical, but as your scripts become larger and more complex it can become very helpful to be able to look at the variable name and understand its purpose. For instance you assign the first number to a variable named a. If your script were 2000 lines long and I were reading it, it is unlikely that I would remember around line 1500 that a held the information that we got from the user around line 5. If however it were named something like strFirstNumberFromUser or even strFirstNumber, it would be much more likely that I would know what it held without tracing back through 1500 lines of code to find it's definition.

    You are off to a good start. I think the next step that would be fun for you to learn is to make it able to handle someone entering "2 + 2" and giving the right answer.
    "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
    Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
    http://www.visualbasicscript.com/m_47117/tm.htm
    #2
      ehvbs

      • Total Posts : 3312
      • Scores: 110
      • Reward points : 0
      • Joined: 6/22/2005
      • Location: Germany
      • Status: offline
      Re:A simple calculator. Thursday, August 19, 2010 8:49 AM (permalink)
      0
      You shouldn't mix top-level code and functions/subs - move them
      either to the top or the bottom of your file.

      A function should return a value - if you don't want to return
      something, use a Sub.

      Don't use global variables (a,b); never mix globals and parameters
      in a Sub/Function - pass all the info needed in the Sub/Function as
      parameters.
      #3
        logan

        • Total Posts : 2
        • Scores: 0
        • Reward points : 0
        • Joined: 8/19/2010
        • Status: offline
        Re:A simple calculator. Friday, August 20, 2010 10:04 AM (permalink)
        0
        Thanks for the advice, guys. The reason I put the function in the middle was because I wasn't sure if it would work if I called for it before it was written. And, I wasn't sure if I had to declare the variables first. Again, thanks for the help. 

        EDIT:
        I edited the variable names, and changed the function to a sub. Also, I moved it to the bottom of the code. It still works, and that's the important thing.


        EDIT2:
        Since the code thing isn't working correctly, I'll just link to the code. http://pastebin.com/6GUwa7Vm This has Option Explicit, also.





        <message edited by logan on Friday, August 20, 2010 11:22 AM>
        #4
          ebgreen

          • Total Posts : 8088
          • Scores: 95
          • Reward points : 0
          • Joined: 7/12/2005
          • Status: offline
          Re:A simple calculator. Tuesday, August 24, 2010 4:44 AM (permalink)
          0
          The changes are good. Now look at what you ask the user to do then ask yourself what would happen if they did exactly what you said. Then think about how to fix it.
          "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
          Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
          http://www.visualbasicscript.com/m_47117/tm.htm
          #5
            MrDinoC

            • Total Posts : 15
            • Scores: 0
            • Reward points : 0
            • Joined: 9/17/2010
            • Status: offline
            Re:A simple calculator. Tuesday, September 21, 2010 2:52 AM (permalink)
            0
            Hi, 
            the only thing you need to put in now so that it calculates the numbers together properly is to add in the conversion function.
            So your statements must look like this.

            a=CInt(inputbox("What is the number you want first in the equation?") )




            #6
              ebgreen

              • Total Posts : 8088
              • Scores: 95
              • Reward points : 0
              • Joined: 7/12/2005
              • Status: offline
              Re:A simple calculator. Tuesday, September 21, 2010 2:59 AM (permalink)
              0
              What if you want to multiply decimals?
              "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
              Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
              http://www.visualbasicscript.com/m_47117/tm.htm
              #7
                CØLLØSUS

                • Total Posts : 21
                • Scores: 0
                • Reward points : 0
                • Joined: 1/3/2011
                • Status: offline
                Re:A simple calculator. Thursday, January 06, 2011 10:25 PM (permalink)
                0
                this Works, Check out mine
                #8

                  Online Bookmarks Sharing: Share/Bookmark

                  Jump to:

                  Current active users

                  There are 0 members and 1 guests.

                  Icon Legend and Permission

                  • New Messages
                  • No New Messages
                  • Hot Topic w/ New Messages
                  • Hot Topic w/o New Messages
                  • Locked w/ New Messages
                  • Locked w/o New Messages
                  • Read Message
                  • Post New Thread
                  • Reply to message
                  • Post New Poll
                  • Submit Vote
                  • Post reward post
                  • Delete my own posts
                  • Delete my own threads
                  • Rate post

                  2000-2012 ASPPlayground.NET Forum Version 3.8
                  mbt shoes www.wileywilson.com