Micro Focus QTP (UFT) Forums
Error in getting sum of numbers program - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: VB Scripting/Descriptive Programming (https://www.learnqtp.com/forums/Forum-VB-Scripting-Descriptive-Programming)
+--- Thread: Error in getting sum of numbers program (/Thread-Error-in-getting-sum-of-numbers-program)



Error in getting sum of numbers program - QTpier - 07-10-2014

hi all

I am new to scripting and i have written program which give sum of the digits in a number as follows:
Code:
Dim num,sum,getdigit
sum=0
getdigit=""
num=Inputbox("Enter a four digit number")
If Len(num) <> 4 Then
Msgbox("Number is not four digit no")
else
For i=0 to Len(num)
getdigit=Mid(num,i+1,1)
'MsgBox(getdigit)
sum = sum+cInt(getdigit)
next
MsgBox("Sum of the digits is " & sum)
End if
I am getting Type mismatch error at
sum= sum + cInt(getdigit)
but I am not able to understand why I am getting this error because i am converting string to number.
Can somebody help me with this?


RE: Error in getting sum of numbers program - kotaramamohana - 07-15-2014

Hi,
i have updated your for loop code, it is working now
Code:
Dim num,sum,getdigit
sum=0
getdigit=""
num=Inputbox("Enter a four digit number")
If Len(num) <> 4 Then
Msgbox("Number is not four digit no")
else
For i=0 to Len(num)-1
getdigit=Mid(num,i+1,1)
'MsgBox(getdigit)
sum = sum+cInt(getdigit)
next
MsgBox("Sum of the digits is " & sum)
End if



RE: Error in getting sum of numbers program - QTpier - 07-18-2014

I understood the error... Error was coming only in the last iteration!!
Thanks for the help.