Jump to content

"Inputbox Calculator"


Recommended Posts

Hi,

I'm trying to make a script that will help adding up certain things e.g. money, bills etc

Basically I want to make a variable that equals an InputBox that asks the user "how many items need to be added". If the user types in, for example, 5 the variable then becomes "5". I want then to use that variables number to then make "5 input boxes" enabling the user to add 5 different items. at the end using a msgbox to show the total

$NumberOfItems = Inputbox("Number of Items", "Enter the number of items you want to add up")
If $NumberOfItems > "1" Then
call("addup")
endif
Func addup()
#cs
This is where i want to have the amount of inputboxes come up.
the amount being what the string of $NumberOfItems variable equals. So the first inputbox comes up, user types in say $20.50.. presses enter, the next inputbox comes up, user types in $15.10 etc etc
after the final inputbox comes up there needs to be a formula that says "first inputbox + second inputbox + third inputbox etc"
then at the end a msgbox appears with the total cost.
#ce
endfunc
Link to comment
Share on other sites

Something like this:

$NumberOfItems = Inputbox("Number of Items", "Enter the number of items you want to add up")
If Int($NumberOfItems) > 1 Then
addup(Int($NumberOfItems))
else
msgbox(64, "", "")
endif
Func addup($items)
Local $Result[$items + 1]
$SUM = 0
For $i = 1 to $items
$Result[$i] = Inputbox("Item " & $i, "Enter the number you want to add up")
$SUM = $SUM + Int($RESULT[$i])
Next
msgbox(64, "", "The sum is: " & $SUM)
$Result = 0
endfunc

----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

Link to comment
Share on other sites

;) OK, I'll explain:

The script first asks you to tell the no. of items you wish to add. However, the value you put inside the input box is a string, so we convert it into an integer with Int() function.

If the no. of items you wish to add is greater than 1 then addup() function is called with the no. of items as single parameter.

Defining addup() function:

First I declare an array containing no. of items + 1 variables (local scope)

I declare a variable $SUM = 0

As the function asks you about inputs through the loop, it changes the value of $SUM. Once the loop is finished, the final value of sum is your answer, which I display through a message box.

Then I free up memory by putting $Result array = 0

Hope this was helpful.

Regards

----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

Link to comment
Share on other sites

Im having trouble understanding arrays, never used them before, seen them in the help files, just havent used them..

So what im guessing is that on the line where you've written "Local $Result[$items + 1]"... That is saying that because $items = 'user input, lets say 4 for example' that $result will occur 4 times? I dont really understand it at all lol especially the + 1 after it.. Is the + 1 representing the next variable? That being $SUM?

Would you be able to go more into detail with the array section please?

Also at the start of the script when you call "addup" you didnt write 'call("addup")' and it still called the func.. Never seen that before.. I have also got to ask one more question.. And that is... Why did you convert the $numberofitems variable to $items? Any particular reason?

Much appreciated!

Link to comment
Share on other sites

Array count starts from 0 and not from 1. That being said, suppose you were to add three things, so logically you would make a 3-element array, ie. $Array[0], $Array[1], $Array[2], but in my script I start my loop from 1 to the no. of quantities ie. 3

So, my loop will modify the elements as: $Array[1], $Array[2], $Array[3]. Surprisingly, $Array[3] doesn't exist, so it will pop-up an error. Using +1, I create an array with 4 elements, so now $Array[3] is valid. The problem is actually with my code and not with Arrays.

Search for and read about Call() function properly. Also, I'll give you an example of how functions work:

DisplayName("MKISH")
Func DisplayName($NAME)
msgbox(64, "The name is: " & $NAME)
EndFunc

So, I declared a DisplayName function with $NAME (Name of person) as parameter. When this function is called with "MKISH" as parameter, the output is a message box with my name.

Bye...

----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...