Qbasicnews.com
February 23, 2020, 08:00:27 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 [2] 3
  Print  
Author Topic: Input 2 digit numbers  (Read 9184 times)
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #15 on: November 12, 2003, 10:20:38 AM »

You should use INKEY$ for that sort of thing.

I was thinking something much different:

Code:

INPUT "gimme a number", n$

len.n% = LEN(n$): DIM digit(len.n%) AS INTEGER

FOR i% = 1 TO len.n%
temp$ = MID$(n$, i%, 1): asc.temp% = ASC(temp$)
IF asc.temp% < 48 THEN EXIT FOR
IF asc.temp% > 57 THEN EXIT FOR
digit%(i%) = asc.temp% - 48
NEXT i%
Logged

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.
Moneo
Na_th_an
*****
Posts: 1971


« Reply #16 on: November 14, 2003, 02:18:30 AM »

Aga,

Your solution, with the embedded validation for numeric, is on the right track, but it still has a few holes in it, as follows:

1) Would it be too much to ask to start off with a DEFINT A-Z so as not to have to append % to the integer variables.

2) Immediately after getting the length of the input string into the len.n variable, you should check that this length is 2, which was part of the specifications. If it is not 2, exit program with an error message. A zero length, result of null input string, would of course cause an error in your version of the code.

3) It's not clear what happens, or what you intended, when a non numeric character is entered and your logic does an EXIT FOR. What is the code after the final NEXT?

P.S. It's amazing how a simple problem can cause so much discussion.
*****
Logged
Anonymous
Guest
« Reply #17 on: November 14, 2003, 03:02:18 AM »

Code:
DO

 CLS

 DIM Number$(2)

 FOR KeyGet  = 1 TO 2
 
  LOCATE 1,1: PRINT "Enter a two-digit number."
 
  DO
   Number$(KeyGet) = INKEY$
  LOOP UNTIL VAL(Number(KeyGet)) => 0

  LOCATE 2, KeyGet: PRINT Number$(KeyGet)

 NEXT

 IF VAL(Num$) > 0 THEN EXIT DO

LOOP

** Rest of code


Guys, I don't think this one can be beat... (After 2 edits. Tongue )

Quote from: "Moneo"
P.S. It's amazing how a simple problem can cause so much discussion.
*****


For sure! Dude prolly ran away n never came back as soon as we started!  Smiley
Logged
relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #18 on: November 14, 2003, 03:18:04 AM »

Let's just say, there are a million and one solutions to any particular problem.  Cheesy
Logged

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
Anonymous
Guest
« Reply #19 on: November 14, 2003, 03:26:12 AM »

nu-uh mines bestest  :rotfl:
Logged
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #20 on: November 14, 2003, 10:35:07 AM »

Quote

1) Would it be too much to ask to start off with a DEFINT A-Z so as not to have to append % to the integer variables.


Yes. I like to keep things clear. I almost always use the % or the &, etc. for variables.

[qupte]
2) Immediately after getting the length of the input string into the len.n variable, you should check that this length is 2, which was part of the specifications. If it is not 2, exit program with an error message. A zero length, result of null input string, would of course cause an error in your version of the code.
[/quote]

True, but third code piece is a general program and works for any length.

Quote

3) It's not clear what happens, or what you intended, when a non numeric character is entered and your logic does an EXIT FOR. What is the code after the final NEXT?


*shrug* If it isn't entirely numeric, a simple "IF i% <= len.n%" will do the trick.

Quote

PS. It's amazing how a simple problem can cause so much discussion.


Yeah, I know! Like the simple matter of arranging electron charge inside a computer!
Logged

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.
dadsherm
Member
*
Posts: 75


« Reply #21 on: November 14, 2003, 03:25:43 PM »

There are different ways to seperate the two(2) digits, but the way I prefer is as follows:
Using the "Locate" function, locate the exact location of the first digit and assign it to the letter "A" and the second digit to the letter "B". Here's an example:
(Assuming that your input message is on line 2 and the "number" reply begins at colum 15)
Locate 2, 15:X = (Screen 2, 15) :A = VAL(CHR$(X))
Locate 2,16:X = (Screen 2, 16): B = VAL(CHR$(X))
This will give you two very distinct values.
If this seems confusing, remember "X" will assume what ever value you give it, even if it had a previous value.
This can also be done in a "loop", but if you're new to the langauge, that can really be confusing.
Logged

adsherm
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #22 on: November 14, 2003, 03:49:09 PM »

You don't need the LOCATEs at all. And the parentheses are misplaced. It should be:

Code:
X = SCREEN(2, 15) :A = VAL(CHR$(X))


This will only work in SCREEN 0, so be careful.
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 #23 on: November 15, 2003, 01:33:43 AM »

Quote from: "dadsherm"
There are different ways to seperate the two(2) digits, but the way I prefer is as follows:
Using the "Locate" function, locate the exact location of the first digit and assign it to the letter "A" and the second digit to the letter "B". Here's an example:
(Assuming that your input message is on line 2 and the "number" reply begins at colum 15)
Locate 2, 15:X = (Screen 2, 15) :A = VAL(CHR$(X))
Locate 2,16:X = (Screen 2, 16): B = VAL(CHR$(X))
This will give you two very distinct values.
If this seems confusing, remember "X" will assume what ever value you give it, even if it had a previous value.
This can also be done in a "loop", but if you're new to the langauge, that can really be confusing.

WELCOME TO THE FORUM, DADSHERM.
1) You didn't mention how you "input" the two digits.
2) You didn't validate for numeric. When you take the VAL it better be numeric, else error.
3) I'm not really convinced of your approach, but I'll give you the benefit of the doubt. By the way, did you test it?
*****
Logged
adosorken
*/-\*
*****
Posts: 3655



WWW
« Reply #24 on: November 15, 2003, 01:42:11 AM »

Quote from: "Moneo"
2) You didn't validate for numeric. When you take the VAL it better be numeric, else error.

Umm...dude...when you use VAL on a non-numeric string, it simply returns zero, not an error.
Logged

I'd knock on wood, but my desk is particle board.
dadsherm
Member
*
Posts: 75


« Reply #25 on: November 15, 2003, 12:04:30 PM »

Quote from: "Moneo"
Quote from: "dadsherm"
There are different ways to seperate the two(2) digits, but the way I prefer is as follows:
Using the "Locate" function, locate the exact location of the first digit and assign it to the letter "A" and the second digit to the letter "B". Here's an example:
(Assuming that your input message is on line 2 and the "number" reply begins at colum 15)
Locate 2, 15:X = (Screen 2, 15) :A = VAL(CHR$(X))
Locate 2,16:X = (Screen 2, 16): B = VAL(CHR$(X))
This will give you two very distinct values.
If this seems confusing, remember "X" will assume what ever value you give it, even if it had a previous value.
This can also be done in a "loop", but if you're new to the langauge, that can really be confusing.

WELCOME TO THE FORUM, DADSHERM.
1) You didn't mention how you "input" the two digits.
2) You didn't validate for numeric. When you take the VAL it better be numeric, else error.
3) I'm not really convinced of your approach, but I'll give you the benefit of the doubt. By the way, did you test it?
*****

Sorry !
I assumed that the person was using the keyboard for the "input",
(groan), other than that it doesn't matter how you're "INPUTing" the numbers.
It's been so long since I've used "INPUT, LINE INPUT,...etc", that it slipped my mind about checking for a numeric expression.
I don't know why I didn't think of adding that to my answer, especially since I do it all the time in the way I program.
(50 lashes with a wet noodle!)
Someone else made the comment that it doesn't come up as an error, and in a way that's right. "0" is not exactly an error, but it also doesn't give you what you're looking for.
My favorite "error" message that Input produces is the good ol'
" Redo from Start?" Well DUH !
As for if I tried this approach, the answer is yes and no.
I use this in my programs, but I never applied it to this program in particular. (Don't you just love these politial answers )
Logged

adsherm
Moneo
Na_th_an
*****
Posts: 1971


« Reply #26 on: November 15, 2003, 07:39:37 PM »

(50 lashes with a wet noodle!) I haven't heard that line in at least 25 years. Either you're an oldtimer, Dadsherm, or the line is so old that it's coming back.

Yes, I noticed your "political" answers. The fact that you've been doing something for years in your programs, doesn't hold any water when you you give "almost correct" examples of these things on the forum. Your batting average in the past doesn't mean a thing when you step up to the plate today.  :wink:

Personally I never get the message "Redo from start", because I NEVER input into a numeric variable. I don't want the user to have to figure out what the message means, especially since I've been working in foreign countries for the past 20 years.
*****
Logged
adosorken
*/-\*
*****
Posts: 3655



WWW
« Reply #27 on: November 15, 2003, 09:27:53 PM »

Quote from: "dadsherm"
Someone else made the comment that it doesn't come up as an error, and in a way that's right. "0" is not exactly an error, but it also doesn't give you what you're looking for.

That someone was ME, don't forget it Wink

j/k

anyways, it's not an error...what you are looking at is a bug, which differs from an error. An error is a flaw in the code that doesn't compile (or in this case, interpret, if you're running in the IDE). A bug is code that executes properly but doesn't produce the desired result. In this case, a return value of 0 may or may not be what you're looking for. If you want to create something that only allows you to input numbers, it's not entirely difficult to write a routine using even something as simple as INKEY$ that checks the ASC value of each key entered. That should be enough of a hint to get anyone moving towards solving this particular problem Wink
Logged

I'd knock on wood, but my desk is particle board.
Anonymous
Guest
« Reply #28 on: November 16, 2003, 10:22:30 PM »

Quote from: "adosorken"
...it's not entirely difficult to write a routine using even something as simple as INKEY$ that checks the ASC value of each key entered. That should be enough of a hint to get anyone moving towards solving this particular problem Wink
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.)  Smiley
Code:
DO

 CLS

 Digs = 2

 REDIM Number$(Digs)

 FOR KeyGet  = 1 TO Digs
 
  LOCATE 1,1: PRINT "Enter a"; STR$(Digs); "-digit number."
 
  DO
   DO: Number$(KeyGet) = INKEY$: LOOP UNTIL Number$(KeyGet) <> ""
  LOOP UNTIL Number$(KeyGet) => 0

  LOCATE 2, KeyGet: PRINT Number$(KeyGet)

  TmpNum = TmpNum + VAL(Number$(KeyGet)) * 10 ^ (Digs - KeyGet)

 NEXT

 IF VAL(TmpNum) > 0 THEN EXIT DO

LOOP

** Rest of code


If you find a better way (or redundant code), share puhlease..  Smiley  later
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #29 on: November 17, 2003, 02:11:20 AM »

Ok, here's a solution which uses my standard function for testing for a pure numeric field. Note: I have not had a chance to test this.
Code:

DEFINT A-Z
DECLARE FUNCTION   NUMSTRICT   (Z$)
cls
START:
Print "Enter a two digit number "; input a$
if len(a$)<>2 or not numstrict(a$) then print "Invalid input":goto start
number1=val(left$(a$,1))
number2=val(right$(a$,1))
print "Number1 =";number1
print "Number2 =";number2
SYSTEM
END
FUNCTION NumStrict (Z$)

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

  NumStrict=0            'Initialize 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 [2] 3
  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!