Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Input 2 digit numbers
#31
Quote:dude i already wrote one a couple of posts ago... only thing is you dont even have to set up an asc statement... (Two boolean operations) you really only need val(Number$) => 0 then at the end make sure that the entire number is greater than 0. i STILL think this is the best way to do it... (slightly optimized from last time.) Smile
Dude...what if someone enters 00 as the two numbers? You're hosed.
I'd knock on wood, but my desk is particle board.
Reply
#32
Code:
if ltrim$(rtrim$(str$(val(a$))))<>a$ then ? "Not numerical"
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#33
Nathan,
I was using that nice, short approach for checking for numerics more than 10 years ago and discovered that there were several things wrong with it, as follows:
1) It doesn't detect a null string, which is not numeric.
2) If the string contains a number with a "D" in it, Basic will think that it's a very large numeric number, and when taking the VAL it will result as equal. I don't remember the exact number having the "D", but the case is that it can happen, and did happen to me during testing. Please take my word for it.

So, based on the above, I abandoned the approach very similar to yours, and opted for the more straightforward approach shown in my post. It may not be short nor very pretty, but it's been running bulletproof in production in hundreds of programs for 10 years.
*****
Reply
#34
Quote:
Cha0s Wrote:dude i already wrote one a couple of posts ago... only thing is you dont even have to set up an asc statement... (Two boolean operations) you really only need val(Number$) => 0 then at the end make sure that the entire number is greater than 0. i STILL think this is the best way to do it... (slightly optimized from last time.) Smile
Dude...what if someone enters 00 as the two numbers? You're hosed.

I dont mean to be a bastard, but... FOLLOW THE CODE NUMNUTS :p

The whole "TmpNum" operation was included only for that purpose.

By the way that example STILL had errors in it...

"LOOP UNTIL Number$(KeyGet) => 0"
should be...
LOOP UNTIL VAL(Number$(KeyGet)) => 0

and
 IF VAL(TmpNum) > 0 THEN EXIT DO
should be
IF TmpNum > 0 THEN EXIT DO

adsorken, it still works with all zeroes though... you should run code b4 u judge it dude.. :roll:
Reply
#35
*slaps forehead*

getting a little testy and disrespectful, are we?

and your code is unncecessarily complex. Hence why I suggested ASC in the first place...simple, clean, and newbies have to learn it anyways. Remember we're in the Newbie Help section here...not the "lemme write out a twenty line program that could easily be done in 4 lines just to show how l33t I think I am" section.
I'd knock on wood, but my desk is particle board.
Reply
#36
Hey now, i could come back and start another flame war, but instead I'll try something more civilized... I challenge you to do what was originally wanted... Get a two digit number(bigger than 0), separate the numbers, and print the output; in less (cleaner?) code than me. Put your money where your mouth is.

P.S. No disrespect... your a way better coder than me! Sad
I want you to win this :p

Code:
DO
CLS
FOR KeyGet = 1 TO 2
  LOCATE 1, 1: PRINT "Enter a 2-digit number."
  DO
   DO: Number$(KeyGet) = INKEY$: LOOP UNTIL Number$(KeyGet) <> ""
  LOOP UNTIL ASC(Number$(KeyGet)) > 47 AND ASC(Number$(KeyGet)) < 58
  LOCATE 2, KeyGet: PRINT Number$(KeyGet)
  TmpNum = TmpNum + VAL(Number$(KeyGet)) * 10 ^ (2 - KeyGet)
  LOCATE KeyGet + 3, 1: PRINT "Number"; KeyGet; "is "; Number$(KeyGet)
NEXT
IF TmpNum > 0 THEN EXIT DO
LOOP
Reply
#37
Considering his original question involved using INPUT, I'd have to say you'd need to go on that instead of using INKEY$. However, to humor you, I've done both.

Code:
CLS
PRINT "Enter a 2-digit number."
FOR KeyGet = 1 TO 2
  DO
    DO: a$(KeyGet) = INKEY$: LOOP WHILE a$(KeyGet) = ""
  LOOP UNTIL ASC(a$(KeyGet)) > 47 AND ASC(a$(KeyGet)) < 58
NEXT KeyGet
PRINT "Number is "; a$(1); a$(2)
and of course, should he want the numeric values of that, it's one more line of code:
Code:
Tens = VAL(a$(1)) * 10: Ones = VAL(a$(2))
Now for his actual question, which was using INPUT, this is how I'd do it:
Code:
CLS
DO
  DO
    INPUT "Enter a 2-digit number"; a$
  LOOP UNTIL LEN(a$) = 2
LOOP UNTIL ASC(LEFT$(a$,1)) > 47 AND ASC(LEFT$(a$,1) < 58 AND ASC(RIGHT$(a$,1)) > 47 AND ASC(RIGHT$(a$,1) < 58
PRINT "Number is "; LEFT$(a$, 1); RIGHT$(a$, 1)
and for the actual numeric values...
Code:
Tens = VAL(LEFT$(a$, 1)) * 10: Ones = VAL(RIGHT$(a$, 1))

EDIT: Stupid international keyboard. Tongue
I'd knock on wood, but my desk is particle board.
Reply
#38
Nice, but you forgot the possibility that both numbers are 0.
Reply
#39
No I didn't. The ASCII value of 0 is 48. And I don't recall seeing anything about filtering out 00 in any way. My code will grab two digits and two digits only, it will ignore anything except 0 thru 9. Which is what he wanted in the first place.

And now that I look his question over again, I see something that doesn't need to be there, which is the multiplication of the first variable. Since he wants the input split into digits, there is no reason to multiple the first value by 10.
I'd knock on wood, but my desk is particle board.
Reply
#40
Well, if you don't have to worry about both zeroes, and you don't echo print the input (personally i think thats bad coding practice), we essentially... tied Tongue

Code:
CLS
FOR KeyGet = 1 TO 2
LOCATE 1, 1: PRINT "Enter a 2-digit number."
DO
  DO: Number$(KeyGet) = INKEY$: LOOP UNTIL Number$(KeyGet) <> ""
LOOP UNTIL ASC(Number$(KeyGet)) > 47 AND ASC(Number$(KeyGet)) < 58
LOCATE KeyGet + 2, 1: PRINT "Number"; KeyGet; "is "; Number$(KeyGet)
NEXT

Good Game! Smile
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)