Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need help with check digits

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Is there a function or way to separate a 4 digit user-inputed variable into 4 separate variables? I need this for a check digit program. I cant really think of a better way to describe this....sorry
 
if inputting via string variable you can use left$ or mid$.
if inputting via number you work at extracting the values. I use int(v*.001) for the first postion. Ed Fair
unixstuff@juno.com
Any advice I give is my best judgement based on my interpretation of the facts you supply. Help increase my knowledge by providing some feedback, good or bad, on any advice I have given.
 
Probably the easiest way is to set up an array with 4 subscripts. If you've got a 4 digit number variable then I'd change it to a string. Then extract the numbers.

OPTION BASE 1

DIM Number AS INTEGER
DIM StrNum AS STRING
DIM Count AS INTEGER
DIM MyArray(4) AS STRING

Number = 2468

'*** next line needed as QBasic puts a space in front ***
'*** of the number when it converts it to a string ***

StrNum$ = LTRIM$(STR$(Number))

FOR Count = 1 TO 4
MyArray$(Count) = MID$(StrNum$, Count, 1)
PRINT "MyArray"; Count; "= "; MyArray$(Count)
NEXT Count

If you need the individual numbers as numerics you can either define MyArray as an integer and in the code above use :-

MyArray(Count) = VAL(Mid$(StrNum$, Count, 1))

or you can change the strings in MyArray$ and convert them to numbers when you use them :-

FOR Count = 1 to 4
PRINT VAL(MyArray$(Count)) + 2
NEXT Count

Ray
 
As noted, the easiest way to do this is by converting the number into a string and then parsing out the individual digits according to their position in the string....
[tt]
MyNumber& = 1234
MyStringNum$ = LTrim(Str$(MyNumber&))
Digits = Len(MyStringNum$)
[/tt]

' Make sure the number is less than 5 digits.
' This will trunctate the rightmost digits
' after the fourth digit.[tt]
If Digits > 4 Then
MyStringNum$ = Left$(MyStringNum$, 4)
End If
[/tt]

' Make sure the number is at least 4 digits.
' Using a number with less than 4 digits will
' return zeros until the actual digits are encountered:
' 123 will return 0 1 2 3
' 12 will return 0 0 1 2... and so on.[tt]
MyStringNum$ = Right$("0000" + MyStringNum$, 4)
[/tt]

' Get the individual digits.[tt]
FirstNum& = Val(Mid$(MyStringNum$, 1, 1))
SecondNum& = Val(Mid$(MyStringNum$, 2, 1))
ThirdNum& = Val(Mid$(MyStringNum$, 3, 1))
FourthNum& = Val(Mid$(MyStringNum$, 4, 1))

PRINT FirstNum&, SecondNum&, ThirdNum&, FourthNum&
[/tt]


You could always do this the hard way... That is, by retrieving the individual digits using arithmetic....

' Make sure the number is less than 5 digits.[tt]
If Digits > 4 Then
MyNumber& = MyNumber& \ (10 ^ (Digits - 4))
End If
[/tt]

' Make sure the number is at least 4 digits.
' (multiply by 10000 then extract by getting the
' integer portion after dividing by an appropriate number).[tt]
TmpNumber& = MyNumber& * 10000
FirstNum& = TmpNumber& \ 10000000
Tmp& = FirstNum& * 10000000
SecondNum& = (TmpNumber& - Tmp&) \ 1000000
Tmp& = (FirstNum& * 10 + SecondNum&) * 1000000
ThirdNum& = (TmpNumber& - Tmp&) \ 100000
Tmp& = ((FirstNum& * 100) + (SecondNum& * 10) + ThirdNum&) * 100000
FourthNum& = (TmpNumber& - Tmp&) \ 10000

PRINT FirstNum&, SecondNum&, ThirdNum&, FourthNum& [/tt]

VCA.gif
 
Are you trying to do this?

7 3 2 4
| | | |
Num1_/ | | \_Num4
/ Num2/ \_Num3

If so, just use the following code:

Str$ = "7324" ' This is the 4 Digit number

If Len(Str$) >= 4 then ' Make sure Str$ has 4 digits
Num1 = Val(Left$(Str$, 1)) ' Set Num1
Num2 = Val(Mid$(Str$, 2, 1)) ' Set Num2
Num3 = Val(Mid$(Str$, 3, 1)) ' Set Num3
Num4 = Val(Mid$(Str$, 4, 1)) ' Set Num4
End if

Simple...
 
Duh! Why didn't we think of that!?! lol. I guess the best solution is always the simplest
 
If you want the numbers right-aligned instead of left-aligned (e.g., so that the first variable is *always* the units place), you can use a series of MOD and integer division operations as follows:
[tt]
DIM digit%(15)
LINE INPUT "Enter a number: ", a$
number& = VAL(a$)

IF number& < 0 THEN
negative% = -1
number& = -number&
ELSE
negative% = 0
END IF

' this loop calculates the digits
i% = 0
DO WHILE number& <> 0
digit%(i%) = number& MOD 10
number& = number& \ 10
i% = i% + 1
LOOP

numDigits% = i%

PRINT &quot;The number is&quot;; numDigits%; &quot;digit&quot;;
IF numDigits% > 1 THEN PRINT &quot;s&quot;;
PRINT &quot; long.&quot;

IF negative% THEN PRINT &quot;The number is negative.&quot;

FOR i% = 0 TO numDigits% - 1
a$ = LTRIM$(STR$(10 ^ i%))
PRINT &quot;The &quot; + a$ + &quot;'s place digit is&quot;; digit%(i%)
NEXT i%
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top