QB / QB64 Discussion Forum      Other Subforums, Links and Downloads
  << Previous Topic | Next Topic >>Return to Index  

Avoiding 'Redo From Start'

March 14 2012 at 4:06 PM
AlGoreIthm  (Login AlGoreIthm)

This Function accepts only a numeral as input: The Challenge - very open ended,
just submit the same thing in an improved form.
Also keep in mind this was whipped up by someone who couldn't possibly be more 'math-deficient'.

==============================================================================

DECLARE FUNCTION NUMERAL ()

s# = NUMERAL
PRINT s#

END

FUNCTION NUMERAL#

num$ = "0123456789."
y% = 3
posi% = 1
PRINT "ENTER a numeral:"

DO
LOCATE y%, posi%: PRINT CHR$(178)
LOCATE 1, 56: PRINT posi%

SLEEP
k$ = INKEY$

IF posi% = 1 AND k$ = "-" OR INSTR(num$, k$) THEN inp$ = inp$ + k$: posi% = posi% + 1
IF k$ = "." THEN num$ = LEFT$(num$, 10): nposi% = posi% - 1: LOCATE 2, 56: PRINT nposi%

IF k$ = CHR$(8) AND posi% > 1 THEN
posi% = posi% - 1
IF posi% = nposi% THEN num$ = num$ + ".": nposi% = 0
inp$ = LEFT$(inp$, posi% - 1)
END IF

LOCATE y%, 1: PRINT inp$; " "

LOOP WHILE k$ <> CHR$(13)

NUMERAL = VAL(inp$)

END FUNCTION




 
 Respond to this message   
AuthorReply

(no login)

This seems to do the same thing...

March 14 2012, 7:42 PM 

s = NUMERAL
PRINT s


FUNCTION NUMERAL
cxx = CSRLIN
cxy = POS(0)
DO
LOCATE cxx, cxy
ky$ = INKEY$
IF ky$ <> "" THEN
SELECT CASE ky$
CASE CHR$(8)
com$ = LEFT$(com$, LEN(com$) - 1)
CASE ELSE
com$ = com$ + ky$
END SELECT
END IF
PRINT "Number of digits :"; LEN(com$)
PRINT com$; CHR$(178); SPACE$(1)
LOOP UNTIL ky$ = CHR$(13)
NUMERAL = VAL(com$)
END FUNCTION

 
 Respond to this message   
AlGoreIthm
(Login AlGoreIthm)

nope - not even close

March 16 2012, 3:36 PM 

The problem with your version is that it doesn't meet the challenge of improving the original submission, but also, it accepts the minus sign repeatedly and in any position and the same goes for the decimal point. It also accepts alpha characters while the original only accepts numbers which is the point of the exercise.

Your program works very well 'as-is' but it doesn't prevent unacceptable numeric input from occuring. Please try again.

 
 Respond to this message   

(no login)

What about this?

March 16 2012, 4:00 PM 

s = NUMERAL
PRINT s


FUNCTION NUMERAL

cxx = CSRLIN
cxy = POS(0)
DO
LOCATE cxx, cxy
ky$ = INKEY$
IF ky$ <> "" THEN
SELECT CASE ky$
CASE CHR$(8)
com$ = LEFT$(com$, LEN(com$) - 1)
CASE ELSE
IF ky$ >= CHR$(48) AND ky$ <= CHR$(57) THEN com$ = com$ + ky$
IF ky$ = "." AND INSTR(com$, ".") = 0 THEN com$ = com$ + ky$
IF ky$ = "-" AND LEN(com$) = 0 THEN com$ = com$ + ky$
END SELECT
END IF
PRINT "Number of digits :"; LEN(com$)
PRINT com$; CHR$(178); SPACE$(1)
LOOP UNTIL ky$ = CHR$(13)
NUMERAL = VAL(com$)
END FUNCTION

 
 Respond to this message   

(no login)

More structured version...

March 16 2012, 4:12 PM 

s = NUMERAL
PRINT s

FUNCTION NUMERAL

cxx = CSRLIN
cxy = POS(0)
DO
_limit 60
LOCATE cxx, cxy
ky$ = INKEY$
IF ky$ <> "" THEN
num = ASC(ky$)
SELECT CASE num
CASE 8
com$ = LEFT$(com$, LEN(com$) - 1)
CASE 48 TO 57
com$ = com$ + ky$
CASE 46
IF INSTR(com$, ".") = 0 THEN com$ = com$ + ky$
CASE 45
IF LEN(com$) = 0 THEN com$ = com$ + ky$
END SELECT
END IF
PRINT "Number of digits :"; LEN(com$)
PRINT com$; CHR$(178); SPACE$(1)
LOOP UNTIL ky$ = CHR$(13)
NUMERAL = VAL(com$)
END FUNCTION

Also, how can you have multiple conditions with select case?

 
 Respond to this message   

(Login MCalkins)
Moderator

Re: More structured version...

March 17 2012, 4:28 AM 

>Also, how can you have multiple conditions with select case?

You mean on the same CASE? Use comma as a separator:

CASE 1,3,5,7, is <=-5, 100 to 200, 205

Regards,
Michael

 
 Respond to this message   

(no login)

*Oh Ok.. Thanks

March 17 2012, 5:30 AM 

*

 
 Respond to this message   
AlGoreIthm
(Login AlGoreIthm)

Much better

March 17 2012, 1:28 PM 


this line

com$ = LEFT$(com$, LEN(com$) - 1)

gives an 'Illegal Function Call' Error when backspace tries
to go back before the first digit - getting there ...

 
 Respond to this message   

(no login)

Re: Much better

March 17 2012, 2:10 PM 

I don't get that error but this should fix it :


s = NUMERAL
PRINT s


FUNCTION NUMERAL

cxx = CSRLIN
cxy = POS(0)
DO
_LIMIT 60
LOCATE cxx, cxy
ky$ = INKEY$
IF ky$ <> "" THEN
num = ASC(ky$)

SELECT CASE num
CASE 8
IF LEN(com$) > 0 THEN com$ = LEFT$(com$, LEN(com$) - 1)
CASE 48 TO 57
com$ = com$ + ky$
CASE 46
IF INSTR(com$, ".") = 0 THEN com$ = com$ + ky$
CASE 45
IF LEN(com$) = 0 THEN com$ = com$ + ky$
END SELECT
END IF
PRINT "Number of digits :"; LEN(com$)
PRINT com$; CHR$(178); SPACE$(1)
LOOP UNTIL ky$ = CHR$(13)
NUMERAL = VAL(com$)
END FUNCTION

 
 Respond to this message   
AlGoreIthm
(Login AlGoreIthm)

Final version..?

March 23 2012, 1:52 PM 

I admit to borrowing from pmarathe's early version - it was enough to inspire a
change that helped get rid of variables posi% and nposi%.

DECLARE FUNCTION NUMERAL# (x%, y%)

' x% and y% are coordinate values for screen location

x% = 5: y% = 6: s# = NUMERAL#(x%, y%)

'LOCATE 15, 2: PRINT s# ' test line to validate that s# is identical to the input

END

FUNCTION NUMERAL# (x%, y%)

LOCATE y%, x%: PRINT "ENTER a numeral:"
num$ = "0123456789"

WHILE k$ <> CHR$(13)

LOCATE y%, x% + 17: PRINT inp$; CHR$(178); " ";
k$ = INKEY$
IF (LEN(inp$) = 0 AND k$ = "-") OR INSTR(num$, k$) OR (k$ = "." AND INSTR(inp$, k$) = 0) THEN inp$ = inp$ + k$
IF k$ = CHR$(8) AND LEN(inp$) > 0 THEN inp$ = LEFT$(inp$, LEN(inp$) - 1)

WEND

LOCATE y%, x% + 17: PRINT inp$; " "
NUMERAL# = VAL(inp$)

END FUNCTION

 
 Respond to this message   

(no login)

When does 'redo from start' occur?

March 17 2012, 2:13 PM 

I've never seen this error.. what is it? I'm using QB64, by the way. Is that why I don't get that error?

 
 Respond to this message   
AlGoreIthm
(Login AlGoreIthm)

Re: When does 'redo from start' occur?

March 17 2012, 2:29 PM 

It could be the reason - not familiar at all with QB64.
'Redo From Start' is an error message the user will get while running QBasic as they attempt to enter non-numeric input, when only numeric input is acceptable (ex: in a numeric variable). When this occurs, the Input fails and the user must re-enter his input from the beginning.

The idea here is to set up a simple Input Function that will only accept properly formatted numeric input, and will never need to display an error message; if the input is all-numeric only and properly formatted, it is correct and can be processed.

 
 Respond to this message   

(no login)

Re: When does 'redo from start' occur?

March 17 2012, 2:38 PM 

In QB64 it only allows numeric input when a numeric variable is being used...

 
 Respond to this message   

(Login rpgfan3233)

Re: When does 'redo from start' occur?

March 17 2012, 2:47 PM 

There are two situations in which I know it occurs for certain:
- Using INPUT with a string containing a comma
- Trying to INPUT a string when a numeric variable is used

The last case is one that QB64 eliminates by limiting the characters you can type when trying to store a numeric value.

The first case, however, is a bit more difficult. INPUT was designed to allow you to enter several items at once. For example, you could enter a name and age:
INPUT "Enter your name and age, separated by a comma: ", name$, age%

The problem arises when you do something like this:
INPUT "Enter your name (Last, First): ", name$

Ideally you would use the comma in the string to separate the names later. This limits the number of reads you would need to do. In QBasic, this was definitely an issue due to limited system resources, and string operations may have taken less time. However, if you try to enter the name in the last case:

Kitsune,Chrono

You would end up getting a "Redo from start" message because there are two items there, not one.

Ultimately, there is a rule: use LINE INPUT for strings and INPUT for numbers. And back in QBasic, you should have used LINE INPUT for numbers and parsed the string yourself actually. After all, how can you store 3.45 in an integer variable?

 
 Respond to this message   

(no login)

*Oh Ok.. Thanks

March 17 2012, 2:50 PM 


 
 Respond to this message   
Current Topic - Avoiding 'Redo From Start'
  << Previous Topic | Next Topic >>Return to Index