Jump to content
Sign in to follow this  
BlakeOh

Sum of Numbers Entered

Recommended Posts

BlakeOh

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!

Share this post


Link to post
Share on other sites
Marlo

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
  • Like 1

Click here for the best AutoIt help possible.Currently Working on: Autoit RAT

Share this post


Link to post
Share on other sites
Spiff59

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)

Share this post


Link to post
Share on other sites
BlakeOh

Thanks so much guys! What a great community.

This is actually the first part of a larger project and I stumbled a bit. I will play around with all these examples.

Thanks again all.

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.