An VB script questin of adding numbers
-
Wednesday, July 04, 2012 4:37 AM
I want a VB script like
i have an input box which accepts an integer value for ex: integer value = 321, then the script add these numbers 3+2+1 and gives back the output as 5 how to write the VB script for this??
Please help ME
All Replies
-
Wednesday, July 04, 2012 6:20 AM
This should get you started:
iNumber = 371
iHundreds = iNumber\100
iTemp=iNumber mod 100
iTens = iTemp\10 -
Wednesday, July 04, 2012 8:37 AM
The following is th eonly reasonable answer becuse inputbox does not accept or return integer input. It accepts any printable string. To do this we must check the input and be ready for very large numbers so a test that can handle a huge number is required.
We can arbitrarily choose an empty string as an exit.
' add the digits for any length number entered as a string number=inputbox("Enter Number","Number Cruncher","123456789") If number = "" Then MsgBox "No data enered" ELseIf IsNumeric(number) Then for i = 1 to Len(number) x= x + CInt(Mid(number,i,1)) next MsgBox "The SUM of all digits is " & x Else MsgBox "Not a Number - " & number End IfFor the sake of argument this is how we add up the digits of an integer of arbitrary length.
' add the digits for any length number as an integer iNumber = 123456789 sNumber = Cstr(iNumber) for i = 1 to Len(sNumber) x= x + CInt(Mid(sNumber,i,1)) next MsgBox "The SUM of all digits is " & x
Note that this is the same model or algorithm. (pattern)
This is a favorite question in programming 101 courses. It is a test to see if teh student has understood the fundamentalls of sofwtare design. The answer is nearly identical in all languages due to the requirement to take input from a keyboard.
¯\_(ツ)_/¯
- Edited by jrvMicrosoft Community Contributor Wednesday, July 04, 2012 8:39 AM
- Proposed As Answer by Jakob Gottlieb SvendsenModerator Wednesday, July 04, 2012 8:54 AM
- Marked As Answer by Richard MuellerMVP, Moderator Saturday, July 14, 2012 10:43 PM
-
Wednesday, July 04, 2012 9:02 AM
The following is th eonly reasonable answer becuse inputbox does not accept or return integer input.
Your statement is incorrect. The InputBox function will accept anything. Later on VB Script will treat the user's input as a number when an arithmetic operation is performed on it. If a string operation is performed, it will treat it as a string.
-
Wednesday, July 04, 2012 9:10 AM
Here is the same thing in PowerShell:
$s=Read-host Enter a number $x=[int]0 if([System.Int32]::TryParse($s,[ref]$x)){ $sum=0 $s.ToCharArray()|%{$sum+=[int]"$_"} Write-Host "Your SUM is $sum" -fore green }else{ Write-Host "NaN = $s" -fore red -back white }Just in case you need to test numbers in another language:
http://rosettacode.org/wiki/Determine_if_a_string_is_numeric
¯\_(ツ)_/¯
- Edited by jrvMicrosoft Community Contributor Wednesday, July 04, 2012 9:12 AM
-
Wednesday, July 04, 2012 9:26 AM
Instead of all that TryParse stuff, couldn't you just do this?
if ($num -as [int])
Grant Ward, a.k.a. Bigteddy
-
Wednesday, July 04, 2012 9:34 AMThanks for all buddies.....
-
Wednesday, July 04, 2012 9:54 AM
Instead of all that TryParse stuff, couldn't you just do this?
if ($num -as [int])
Grant Ward, a.k.a. Bigteddy
Good call. Mu code shold also be:
[system.decimal]::TryParse($s,[ref]$x)
The int is too small.
So this would be shorter:
if('12345678901234567890' -as [system.decimal]){'Its an int'}else{'its not'}
'-as' is just a shortcut to "TryParse" but is pronabbly moer "PowerShelleese"
¯\_(ツ)_/¯
- Edited by jrvMicrosoft Community Contributor Wednesday, July 04, 2012 9:55 AM
-
Wednesday, July 04, 2012 10:56 AM
The following is th eonly reasonable answer becuse inputbox does not accept or return integer input.
Your statement is incorrect. The InputBox function will accept anything. Later on VB Script will treat the user's input as a number when an arithmetic operation is performed on it. If a string operation is performed, it will treat it as a string.
The result from inputbox is always a string. It is defined as a function returning a string variant.
If you try to use a string as an integer and it contains non-numeric characters you will get an eror. That is one reason the VB programming language includes IsNumeric. IsNumeric allows us to avoid exceptions. Exceptions are assumed to be resource intensive and potentially disruptive in formal programming.
In scripting, exceptions are too often used in favor of good programming discipline. While this is not serious it does lead to sloppy and error prone code.
In VB there is no "treating as" even though it appears that it works that way. In Dartmouth Basic everything was a string and would be "treated as". We call that "coercion" or type conversion.
In Microsft VB everything is a Variant wrapped around a type. Inside the variant is an actual string or integer or object. These things get coerced or converted into other types depending on the operation. A default coercion can screw you up badly if you are not ready for it such as this:
s="123a456"
y=s+0
Now what are you going to do. Your script has just aborted.
Try some inspections of variants.
MsgBox VarType("a123456") MsgBox VarType("123456") MsgBox VarType(123456) MsgBox VarType(1.0) MsgBox VarType(GetObject("winmgmts:"))See. They are all different.
When dealing with numbers that take input from the outside world we need to "cleanse" the input to avoid errors. This is a discipline that you learn designing and building data extraction and converson systems that may get data from human fingers on keyboards.
When humans type into spreadsheets then save years of data in these spreadsheets and then want to do analysis we must cleanse every field.
Design of data loaders always accounts for the cleansing rules. If we allowed type conversion in these systems before the rules were applied we would be introducing huge amounts of error.
¯\_(ツ)_/¯
- Edited by jrvMicrosoft Community Contributor Wednesday, July 04, 2012 11:03 AM
-
Wednesday, July 04, 2012 9:46 PM
Thanks for the lecture. I am quite aware of these things. You appear to ignore that the OP asked a simple question: How to do a sum of digits. He did not ask about data validation, quality checks etc. etc. You then jumped into the thread and turned his simple question into something bigger than Ben Hur. On the other hand your approach is probably justified in view of your well-known habit of committing cruel acts against English words. It leads you to thinking that people cannot be trusted to type 1234 into an InputBox, even if it is only for a simple exercise in an introductory course in programming! ;-)Design of data loaders always accounts for the cleansing rules. If we allowed type conversion in these systems before the rules were applied we would be introducing huge amounts of error.
¯\_(ツ)_/¯
-
Wednesday, July 04, 2012 10:37 PM
Oberwald - you did not sum any input of digits. but just a three digit entry. I believe that was just an example and the question was how might we sum a string of digits entered in an input box. There was no spec that said there would always be exactly three digits.
This is the kind of thing we run into all of the time and it is necessary to advise the client as to what is needed to accomplish useful and reliable output. Just working to the exact request is not acceptable in system work. It should not be admissible in administrative scripting either.
Sorry but I posted that for others who might be wondering why your example was not extremely usable in a general sense, although it does work for that one exact example as long as no one enters non-numeric characters into the box.
¯\_(ツ)_/¯

