Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
New to vb6. Struggling on a small program to determine how many 3s there is when a user inputs in a five-digit number. I'm suppose to use a loop for this. Can't figure out how to compare each digit separtely.
Any help/direction would be greatly appreciated.
Thanks.

Convert the number to a string. Then loop through the string using Mid$ counting each digit as a character.
Dim a As String
Dim b As Integer
a = Str(12345)
For b = 1 To Len(a)If Mid$(a, b, 1) = "3" Then
' its a 3
End IfNext
The first character in the string will always be a space for a positive number or - for a negative number.
Stuart

num$ = str$(number)
found% = 0
for c% = 1 to 5
if (mid$(num$, c%, 1) = "3") then
found% = found% + 1
end if
next c%

Wizard-Fred,
A little VB tip for you.
Although you have declared the variables with type characters like $ and % and it works, the ability to do so is only there for backward compatibility with earlier versions of VB.
It is good practise to declare your variables with Dim statements, in fact its a good idea to force variable declaration in the Tools > Options menu of VB. Forcing variable declaration helps with debugging with more complex programmes.
With type character it is to easy to miss type a variable and produce all sort of strange bugs without throwing up errors messages.
Or to have number defined as a variant because you forgot to insert the integer type character. A variant is 64 bits while an integer is 16 bits.
Stuart

Bob, Stuart
First a slight correction
line 3 change - for c% = 1 to len(num$)As Stuart points out correctly conversion from number to string has a position for an implied sign.
The reason I didn't catch it is that I normally input everything as strings and convert during or after validation.I did not intend for my example to be the final code, only a skeleton. Stuart you are right in that variables should be declared up front. Actually I do very little coding in VB. I have progressed through various BASIC's; HP3000Basic, AlphaBASIC, Commodore BASIC, MBasic, CB80, CB86, BASICA(GWBASIC), MS BusinessBasicCompiler, MS QuickBasicCompilers, Summit Basic, True Basic, Stamp Basic, AVRBasic, Casio Basic, Radio Shack Basic, and I have tried almost every downloadable variant. I presently program mostly in PowerBasic and various dBase3 variants. I have VB6, VC++, VFoxPro as well as Borland C, COBOL, Fortran, Java, Pascal, LOGO, & Assembler. So I know a little about much, and more about BASIC. My other interest is client/server databases, MySQL, DB2, PowerBuilder, PostgreSQL, MaxDB/SAP. Mostly open source, or free.

![]() |
where can i buy old versi...
|
Need help writting BASIC ...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |