"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
endif
#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```

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
else
msgbox(64, "", "")
endif
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

Share on other sites

Yes! This is exactly what I was after!

would you mind going through your script and explaining how it works?

Thanks

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.

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

Regards

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

:bye: Hey there, was I helpful?

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

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

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!

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

Create an account

Register a new account