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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check string for occurance of a numeric value

Status
Not open for further replies.

PhilBreau

Technical User
Dec 14, 2001
108
CA
I need to read the contents of a file. I may be using fgets. I need to verify if the contents of the string is a 4 digit number.

sample

.
.
.
while not feof 0

fgets 0 fromfile

; this is where I need some help


endwhile
.
..

in the fromfile string, there could be a 4 digit number. How can I check? Disregard the string index. Assume the string fromfile has a length of 4.


Thank you
 
Here is a function I wrote for someone else here a couple months ago that should do the trick:

;Returns a zero if the passed string contains non-numeric characters,
;a one otherwise
func IsNum : integer
param string sNum
integer iLen, iCount, iVal

strlen sNum iLen
for iCount = 0 upto iLen-1
strgetc sNum iCount iVal
if ((iVal < 48) || (iVal > 57))
return 0
endif
endfor
return 1
endfunc

aspect@aspectscripting.com
 
I tried something that seems to work, but I'm not sure if it is the best way.

.
.
.
atoi fromfile i

if i>999&&i<10000
usermsg &quot;%s is a 4 digit number&quot; fromfile
endif
.
.
.


In C I believe there is a command isdigit. Is there something comparable in Aspect?
 
Thank you for your reply knob. I was probably in the middle of sending my own reply back after you sent yours. I do like yours. It verifies the ascii value of each character in the string 48 to 57 ie 0 through 9
 
I've had a similar problem using rgets. I use rgets to grab a number from the screen. The problem is that I never know the exact length of that number. If I use rget sLine 3 and the number is 100 I'm fine. If I use rget sLine 3 and the number is 5 I grab some characters with it. Is there anyway I can only grab numbers so I can store them in a string?
 
One thing you could try is using the set aspect rgetchar to set the character that rget ends on to a space, so you only get the incoming number. I haven't tested this, but it seems feasible to me.


aspect@aspectscripting.com
 
How do I specify a space? I tried the folowing:

set aspect rgetchar STRIP
set aspect rgetchar &quot; &quot;
set aspect rgetchar STRIP &quot;|&quot;

I keep getting &quot;invalid integer&quot;. I know the default is ASCII 13 but I'm already removing that after the rget

if waitfor &quot;[404] &quot; 10
rget sLinea 4
strreplace sLinea &quot;`r&quot; &quot;&quot;
strreplace sLinea &quot;`n&quot; &quot;&quot;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top