Qbasicnews.com
February 26, 2020, 12:09:01 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: How to convert string to integer?  (Read 9360 times)
Yosuke_
Member
*
Posts: 29


« on: December 02, 2004, 02:55:20 PM »

Hello!
When using input and entering invalid values, qbasic shows rendo from start? I want to ask user for two integers saparated by commas and they will be stored into a string variable. Then I will simply read them and change them to integers, if the input will be invalid it will show something else, not rendo from start? What is the function to convert string to an integer? thank you!
Logged
adosorken
*/-\*
*****
Posts: 3655



WWW
« Reply #1 on: December 02, 2004, 02:56:08 PM »

Code:
VAL
Logged

I'd knock on wood, but my desk is particle board.
pr0gger
I hold this place together
*****
Posts: 775


« Reply #2 on: December 11, 2004, 02:39:51 PM »

You don't even have to go that far.  Smiley  When you use INPUT, you can input two variables at once:

Code:
INPUT A,B


When you run this, the user can type in any two numbers separated by a comma, and they will be stored into a and b.

Try it:

Code:
CLS
INPUT a,b
PRINT a
PRINT b


--j_k
Logged

size=9]"To announce that there must be no criticism of the president, or that we are to stand by the president, right or wrong, is not only unpatriotic and servile, but is morally treasonable to the American public." -- Theodore Roosevelt[/size]
Moneo
Na_th_an
*****
Posts: 1971


« Reply #3 on: December 11, 2004, 09:38:48 PM »

The trouble with VAL is that it will ignore any numbers after an invalid character.

The trouble with INPUT is that if there are any invalid characters in the input, you will get the message "Redo from start".

So, you must get each input number into a separate string. Forget about INPUT A,B. Do INPUT A$ and later INPUT B$.
This allow you to validate the supposed numeric data that's in A$ or B$.

You've heard that old line "Garbage in, garbage out", right? Well, get in the habit of always checking your input, or else you'll get garbage, and you're program will look pretty silly.

I'm including a function called NUMSTRICT which validates a string for strictly numerics with no decimas. For example, you can check A$ for numeric as follows:

if not NumStrict(A$) then print "First number is not numeric":system

After you've validated the number in the string and you're ready to convert it to an interger (like you said) by using VAL, you better check the number while it's still a string to make sure that it's not bigger than the max of 32k which fits into an integer, otherwise you're program will hang up with an overflow error on the VAL instruction.

Code:
FUNCTION NumStrict (Z$)

REM *
REM *** (NUMSTRICT) - CHECK FOR STRICTLY NUMERIC ONLY (NO NULL NO DECIMAL)
REM *

  NumStrict=0         'Init to False

  IF Z$="" THEN EXIT FUNCTION

  FOR X = 1 TO LEN(Z$)
      A=ASC(MID$(Z$,X,1))
      IF A<48 OR A>57 THEN EXIT FUNCTION
  NEXT X

  NumStrict = -1          'True

END FUNCTION

*****
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!