Posts: 54
Threads: 16
Joined: Jan 2003
Another stupid quetion from me....
I thought str$ was making numeric values into strings. But why doesn't the programm recognize it as a string??? for example, this does not work (when typing an 1):
input a
b$=str$(a)
if b$="1" then print "it's a one!!!"
Posts: 3,368
Threads: 195
Joined: Jan 2003
for the very obscure reason that adding str$(a) in a print string will not crunch up print "the word is"+str$(a) to "the word is5", Microsoft made str$ add a space to the left after it converted the number to a string.
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 54
Threads: 16
Joined: Jan 2003
ok.
So the problem is solved when using b$=" 1" ....thanks
Posts: 6,419
Threads: 74
Joined: Mar 2002
I'd prefer to trim the space: Use
Code:
number$ = LTRIM$(STR$(number%))
And you'll have "1", "2", "69" ... and such.
Posts: 2,020
Threads: 24
Joined: Jun 2002
the reason for that is that that space is for negative numbers. if the number's negative, a hyphen will be in place of the space. nathan's trick (though nathan didnt invent it) should alleviate all confusion
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Posts: 6,419
Threads: 74
Joined: Mar 2002
NO YOU'RE WRONG!! I invented that trick... uh... I promise... :rotfl: :rotfl: :rotfl: :rotfl:
Posts: 3,368
Threads: 195
Joined: Jan 2003
then you should have a + for all positives!
But you don't, do you!! :red_marvin:
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 63
Threads: 10
Joined: Jan 2003
Hi banAnnA, remember also, if you ask for a numeric input, and someone, (or you) enter a letter, you'll get a "redo from start" message.
I always handle this by asking for a string to start with, then test it for the number afterwards. Makes for a little neater input, and allows you to set a default value. For example:
Code:
GetInput:
INPUT "Enter number: ",a$
IF a$ = "" THEN a$ = "your default value"
IF a$ = "e" THEN END
IF asc(a$) < 48 or ASC(a$) > 57 THEN GOTO GetInput
a = VAL(a$)
PRINT a
Of course, you can put in any default value, or select any action if someone enters the wrong thing. Just play around with this.
Dex
Posts: 1,845
Threads: 44
Joined: Aug 2002
Er... how's this then? lol
Code:
FUNCTION readValue%
k$ = ""
myval$ = ""
DO
k$ = INKEY$
IF LEN(k$) = 0 THEN k$ = CHR$(1)
IF ASC(k$) >= 48 AND ASC(k$) <= 57 THEN myval$ = myval$ + k$
LOOP UNTIL k$ = CHR$(13)
readValue% = VAL(myval$)
END FUNCTION
Using this function you can read
positive numbers from the keyboard.
Posts: 63
Threads: 10
Joined: Jan 2003
Neo's idea of using a FUNCTION is good, however, it doesn't allow for seeing what you're typeing, which may be important. Here's a slight mod of Neo's code that saves a smidgen of CPU power by not looping when there's no keyboard input:
Code:
FUNCTION readValue
myval$=""
DO
g$ = INPUT$(1)
IF ASC(g$) > 47 AND ASC(g$) < 58 THEN
myval$ = myval$ + g$
LOCATE 4, 4 : PRINT myval$' The numbers 4,4 can be whatever you want.
END IF
LOOP UNTIL g$ = CHR$(13)
readValue = VAL(myval$)
END FUNCTION
The important thing is, there's a lot of different ways to do this.
Dex