Jump to content

Sum of Numbers Entered


Recommended Posts

Hi All,

I'm new to AutoIt and loving it thus far. I have a (probably) very simple question for a script I am trying to create:

What I'm doing is creating a simple GUI where a value is entered and the digits are added together and a new value is presented. What I cannot figure out how to do is add consecutive digits, for example:

User enters 1234

User is presented with 10 (1+2+3+4)

any help is appreciated!

Link to comment
Share on other sites

Dan beat me to it with the StringSplit method :idea:

Dim $sNumbers = "123456789" ;Our numbers we want to add together.
Dim $aNumbers = StringSplit($sNumbers, "") ;split te numbers up into an array so: [1] = 1, [2] = 2, etc
Dim $iSum ;this variable will hold the answer

For $I = 1 To $aNumbers[0]
    ;Go through each number in our array and add it to our sum
    $iSum += $aNumbers[$I]
Next

MsgBox(64, "Addition", $sNumbers & " added together is " & $iSum) ;Display the answer
Click here for the best AutoIt help possible.Currently Working on: Autoit RAT
Link to comment
Share on other sites

Another way to do the same thing, but with an array:

#Include <Array.au3>
$iIn = InputBox("Testing", "Enter numbers")
$aString = StringSplit($iIn,"")
MsgBox(0, "Results", Execute(_ArrayToString($aString,'+',1)) & " (" & _ArrayToString($aString,'+',1) & ")")

Clever use of the _ArrayToString() delimiter and Execute(). Kudos.

(also possibly the first useful implementation of Execute() I've seen in 2 years lol)

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...