Qbasicnews.com
February 23, 2020, 08:45:16 AM
 Pages: 1 2 [3]
 Author Topic: Input 2 digit numbers  (Read 9187 times)
*/-\*

Posts: 3655

 « Reply #30 on: November 17, 2003, 04:24:33 PM »

Quote from: "Cha0s"
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.)

Dude...what if someone enters 00 as the two numbers? You're hosed.
 Logged

I'd knock on wood, but my desk is particle board.
na_th_an
*/-\*

Posts: 8244

 « Reply #31 on: November 17, 2003, 08:47:23 PM »

Code:
if ltrim\$(rtrim\$(str\$(val(a\$))))<>a\$ then ? "Not numerical"
 Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Moneo
Na_th_an

Posts: 1971

 « Reply #32 on: November 18, 2003, 02:09:17 AM »

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.
*****
 Logged
Anonymous
Guest
 « Reply #33 on: November 18, 2003, 10:32:45 PM »

Quote from: "Cha0s"
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.)

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:
 Logged
*/-\*

Posts: 3655

 « Reply #34 on: November 19, 2003, 04:58:05 AM »

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.
 Logged

I'd knock on wood, but my desk is particle board.
Anonymous
Guest
 « Reply #35 on: November 19, 2003, 05:33:18 AM »

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!
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
 Logged
*/-\*

Posts: 3655

 « Reply #36 on: November 19, 2003, 05:53:23 AM »

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.
 Logged

I'd knock on wood, but my desk is particle board.
Anonymous
Guest
 « Reply #37 on: November 19, 2003, 06:11:21 AM »

Nice, but you forgot the possibility that both numbers are 0.
 Logged
*/-\*

Posts: 3655

 « Reply #38 on: November 19, 2003, 06:19:20 AM »

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.
 Logged

I'd knock on wood, but my desk is particle board.
Anonymous
Guest
 « Reply #39 on: November 19, 2003, 06:32:45 AM »

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

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!
 Logged
Clark
New Member

Posts: 1

 « Reply #40 on: December 16, 2003, 09:20:33 AM »

Could you give me an example of when you would want to use the "val()" function with a numeric string? Wouldn't this be completely pointless?
 Logged
Mango
Wandering Guru

Posts: 360

 « Reply #41 on: December 16, 2003, 11:01:02 AM »

Quote from: "Clark"
Could you give me an example of when you would want to use the "val()" function with a numeric string? Wouldn't this be completely pointless?

It's often wise to get user-input as a string, and then parse that string to numbers/symbols/text/whatever.
 Logged
Meg
Ancient QBer

Posts: 483

 « Reply #42 on: December 16, 2003, 11:30:54 AM »

Without local echo:
Code:
1 PRINT "Enter a two digit number: ";
2 k\$ = INPUT\$(1)
3 IF k\$ < "0" OR k\$ > "9" THEN GOTO 2 ELSE Num% = 10 * VAL(k\$)
4 k\$ = INPUT\$(1)
5 IF k\$ < "0" OR k\$ > "9" THEN GOTO 4 ELSE Num% = Num% + VAL(k\$)

With local echo:
Code:
1 PRINT "Enter a two digit number: ";
2 k\$ = INPUT\$(1)
3 IF k\$ < "0" OR k\$ > "9" THEN GOTO 2 ELSE Num% = 10 * VAL(k\$): PRINT k\$;
4 k\$ = INPUT\$(1)
5 IF k\$ < "0" OR k\$ > "9" THEN GOTO 4 ELSE Num% = Num% + VAL(k\$): PRINT k\$

*peace*

Meg.
 Logged
Moneo
Na_th_an

Posts: 1971

 « Reply #43 on: December 17, 2003, 01:47:43 AM »

Trust Meg to come up with a nice clean approach.
Nice work, Meg.
Better late than never. :wink:
*****
 Logged
Anonymous
Guest
 « Reply #44 on: December 17, 2003, 03:46:20 AM »

some dude: never send a lady to do a mans job

me: hell no *smack*

meg you rock
 Logged
 Pages: 1 2 [3]