Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Input 2 digit numbers
#21
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!
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
#22
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.
adsherm
Reply
#23
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.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#24
Quote: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?
*****
Reply
#25
Quote: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.
I'd knock on wood, but my desk is particle board.
Reply
#26
Quote:
dadsherm Wrote: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 Ü )
adsherm
Reply
#27
(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.
*****
Reply
#28
Quote: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
I'd knock on wood, but my desk is particle board.
Reply
#29
Quote: ...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.) Smile
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.. Smile later
Reply
#30
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
*****
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)