Qbasicnews.com

Full Version: Question about numeric condition in IF ...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
How do i make a condition for IF THEN statement where if the data entered is not a number, or if it's an empty input, it'd go back to the input, somethin like this:

Code:
x as integer
0 input : x
   IF x not integer THEN
   goto 0
   ELSEIF x = ""
   goto 0
   end if
end

thanks alot
Quote:x as integer 'Where's the DIM?
0 input : x 'what the hell is this? What programming language are you using?
IF x not integer THEN 'There is no way to do this intrinsically, but it's something that's easy to replicate with other functions. So there really isn't a need for this. Programming languages have it though for lazy asshats. :-)
goto 0
ELSEIF x = ""
goto 0
end if
end


This is how to do it: first, ask for a string. Then check if it's a number/(or an integer), and if it is, then convert it to integer format.

Code:
DECLARE FUNCTION isinteger%(astring AS STRING)

SCREEN 9
DIM string1 AS STRING

DO

PRINT "Please enter an integer from -32768 to 32767.";
INPUT " ", string1
string1 = LTRIM$(RTRIM$(string1)) 'Removes extra spaces.
'Now check if it is an integer. I made it into a function.
IF isinteger(string1) THEN
PRINT
PRINT "You entered a signed integer. This is the number you entered:";
PRINT VAL(string1) '(edit: never mind previous comment)
EXIT DO
ELSE
PRINT "You are a poopyhead."
END IF
LOOP

SLEEP

SYSTEM


FUNCTION isinteger%(astring AS STRING)
'Take out leading and trailing 0's.
astring = LTRIM$(RTRIM$(astring))

'Get rid of possible negatives.
IF left$(astring, 1) = "-" THEN
astring = RIGHT$(astring, LEN(astring) - 1)
isnegative% = -1
END IF

'Make sure it isn't an empty string.
IF LEN(astring) = 0 THEN EXIT FUNCTION

'Take out leading 0's, ex. "09".
DO
IF LEN(astring) = 1 THEN EXIT DO
IF left$(astring, 1) = "0" THEN astring = RIGHT$(astring, LEN(astring) - 1)ELSE EXIT DO
LOOP


'1) Integers in QB can only be numbers and must be between -32767 and 32768.

'Check for five character maximum
DIM stringlength AS INTEGER
stringlength = LEN(astring)
IF stringlength > 5 THEN EXIT FUNCTION

'Check that it is all numbers.
FOR i = 1 TO LEN(astring)
test$ = MID$(astring, i, 1)
IF INSTR("0123456789", test$) = 0 THEN EXIT FUNCTION
NEXT i

'Check the strict limit of -32768 to 32767 now.
DIM intlimit(-1 TO 0, 1 TO 5) AS INTEGER, test2 AS INTEGER
intlimit(-1, 1) = 3
intlimit(-1, 2) = 2
intlimit(-1, 3) = 7
intlimit(-1, 4) = 6
intlimit(-1, 5) = 8
intlimit(0, 1) = 3
intlimit(0, 2) = 2
intlimit(0, 3) = 7
intlimit(0, 4) = 6
intlimit(0, 5) = 7

IF LEN(astring) = 5 THEN
FOR i = 1 TO 5
test2% = VAL(MID$(astring, i, 1))
'If it's bigger than the current biggest number possible, exit the function (set isinteger% to 0). If it's less than, exit the for loop: the number is ok. If it's equal to, check the next level to make sure that it isn't overflowing past the limit.
IF test2% > intlimit(isnegative%, i) THEN EXIT FUNCTION
IF test2% < intlimit(isnegative%, i) THEN EXIT FOR
NEXT i
END IF
IF isnegative% THEN astring = "-" + astring
isinteger% = -1
END FUNCTION
Aga,

You covered all the salient elements for validating an input integer number, but I think your solution is a bit complicated. The newbee will just get lost.

Example: all that business usint the intlimit array is cute, but way too hairy. Why not just compare the 5 digit number to 32767 or 32768 if negative?

Also, if it wasn't for your 5 digit restriction in order to use the intlimit array, you wouldn't have to suppress leading zeros.
*****

Anonymous

Code:
Declare Function get_numeric_input() As Integer


Dim result As Integer

result = get_numeric_input()

? "user inputted: " + Str( result )
Sleep


Function get_numeric_input() As Integer

  Do
  
    Dim x As String
    Dim ch As String
    Dim stuck As Integer
    
    stuck = 0
    
    Cls
    Locate 1
    
    Input "input a NUMBER: ", x
    
    If x = "" Then stuck = -1
    
    ch = Left( x, 1 )
    If ( ch >= "a" And ch <= "z" ) Or ( ch >= "A" And ch <= "Z" ) Then
      stuck = -1
      
    End If
    
    If stuck = 0 Then
      Return Val( x )
      
    End If
    
  
  Loop
  
End Function
Moneo:

Quote:Aga,

You covered all the salient elements for validating an input integer number, but I think your solution is a bit complicated. The newbee will just get lost.

Well, yeah, but I made it a function...

Quote:Example: all that business usint the intlimit array is cute, but way too hairy. Why not just compare the 5 digit number to 32767 or 32768 if negative?

Well, you could do it by entering the limits as a string, doing VAL() on them, and comparing, but it would not be as efficient. If you set it to a number and do a > or < comparison for the whole thing, that would be ok but it might create errors since VAL() converts to floating integers I think. It might not, but doing it like that makes you depend on VAL() being able to convert to 99,999.

Quote:Also, if it wasn't for your 5 digit restriction in order to use the intlimit array, you wouldn't have to suppress leading zeros.

Yeah, but I'd rather give the VAL() function something without zeroes.


I shudder to think of what happens to the world if you are ever a programming teacher, cha0s. :evil:

cha0s: remind me to stab you once in a while. First, you're depending on the user having knowledge of the ASCII positioning of "0123456789". Second, please capitalize commands either completely in lowercase or completely in uppercase, not in first-letter-uppercase, or I will have to stab you. Third, this is a QB post and your program will not work in QB. (notice LEFT does not have a "$") Fourth, please use appropriate variables not "ch", "x", and "stuck", or I will have to stab you. (again) FIFTH, you never set your function get_numeric_input to anything! SIXTH,

'( ch >= "a" And ch <= "z" ) Or ( ch >= "A" And ch <= "Z" )'
just says that the number CANNOT be between "A" and "Z" or 'a" and "z". What about "<", " ", ".", etc.? The correct way is:
' (ch >= "9" AND ch <= "0")

SEVENTH, you are not looping through the entire string of x...

EIGHTH, you do not check limits for the INTEGER of -32767 to 32768.

NINTH, I already wrote a function.
Aga,

The best argument for your function is the fact that it works fine. It's hairy, but friendly. :wink:

BTW, while running some tests on alternate ways of comparing to 32767, I think I found a bug in QuickBasic. I'm preparing a post on the "QB Programming Help" sub-forum.
****

Anonymous

yeah, too bad i don't spend around nitpicking useless things, or else i wouldnt have a complex engine to toy around with in my possesion, now would i? *tinker tinker*
I bet it's full of bugs... :roll:

It still matter anyway... if you're going to help a newbie you can't just post junk that doesn't work and expect to get away with it. Tongue
Actually Aga...

Integer (16bit) goes from -32768 to 32767.
Not the other way around.
Quote:Actually Aga...

Integer (16bit) goes from -32768 to 32767.
Not the other way around.
Aga just left that in to make sure you guys were paying attention. :wink:
*****
Pages: 1 2