Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why oh why....STR$?
#1
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!!!"
Reply
#2
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.
Reply
#3
ok.
So the problem is solved when using b$=" 1" ....thanks
Reply
#4
I'd prefer to trim the space: Use

Code:
number$ = LTRIM$(STR$(number%))

And you'll have "1", "2", "69" ... and such.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#5
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
Reply
#6
NO YOU'RE WRONG!! I invented that trick... uh... I promise... :rotfl: :rotfl: :rotfl: :rotfl:
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#7
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.
Reply
#8
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 Big Grin
Reply
#9
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.
Reply
#10
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. Big Grin

Dex
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)