News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu

How do I restrict number of characters on "INPUT" instructions

Started by DRobert, 23:50, 29 September 24

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

DRobert

I had brilliant support on here a few years ago with my football game which has worked successfully for a few years now.

I'm just trying to smooth out a few glitches and need help again.

When I write the program I ensure no team name exceeds 12 letters and this works fine when displaying league tables etc.

If the game player then enters a new team (upon promotion) if more than 12 characters are INPUT this messes around with the format of the league table for instance and pushes the numbers sideways and the columns do not line up.   I presume that an adjustment to coding could restrict the characters actually being printed but would be better if the number of characters could be restricted or pre-set at the INPUT stage.  

Hope this makes sense.  Ive looked through the workbooks for CPC464 plus etc but cant spot the appropriate instructions / hints.

Thanks 


lightforce6128

Some first thoughts:

1) You can use the 'WINDOW #number,left,right,top,bottom' command to define a text window to avoid that the input text overlaps the surrounding screen. With the syntax 'INPUT #windowNumber, variable' you can then ask for an input. It will stay in the window, if required the window will scroll. Afterwards you can trim the variable if necessary.

2) You can use the INKEY() function to ask for single keys. This allows full control, but you also would have to do everything that INPUT does automatically (moving cursor, printing and erasing characters).

I did a short check on the implementation of INPUT in the ROM listing. I did not find a possibility to restrict the number of input characters directly. But maybe I missed something. The code on this level is quite nested, with a big number of CALLs and JPs to other routines.

Jean-Marie

You can use the LEFT$ instruction to brutally truncate the string to 12 characters:

10 MODE 1
20 LOCATE 10,5:INPUT"Team name ";T$
30 T$=LEFT$(T$,12)
40 ? T$

Or you can display an error message, and loop back to the Input request if the length is greater than 12 :

10 MODE 1
20 LOCATE 10,5:INPUT"Team name ";T$
30 IF LEN(T$)>12 THEN LOCATE 10,24:?"12 letters max.":goto 20
40 ? T$

retro space

Quote from: lightforce6128 on 01:41, 30 September 24Some first thoughts:

1) You can use the 'WINDOW #number,left,right,top,bottom' command to define a text window to avoid that the input text overlaps the surrounding screen. With the syntax 'INPUT #windowNumber, variable' you can then ask for an input. It will stay in the window, if required the window will scroll. Afterwards you can trim the variable if necessary.
That looks like pretty advanced BASIC. Does it take character position numbers as units for size?
Teaching computer science on a high school with the CPC, P2000T, Spectrum and C64.

McArti0

Quote from: retro space on 05:50, 30 September 24Does it take character position numbers as units for size?
No. Its coordinates.
CPC 6128, Whole 6128 and Only 6128, with .....
NewPAL v3 for use all 128kB RAM by CRTC as VRAM
One chip drver for 512kB extRAM 6128
TYPICAL :) TV Funai 22FL532/10 with VGA-RGB-in.

GUNHED

Just in case you're using FutureOS...



SHOW AND EDIT A STRING

Short description: With STED you can show, edit or generate a text string.
Label: STED
ROM-number: A
Start address: &FE7D
Jump in conditions:
B = upper border of usable characters. First char that is located below the highest usable ASCII char, &01-&FF.
C = lower border of usable characters, &00 to &FF.
DE = pointer to the first byte of the string which should be edited. Region: &0000 to &FFFF.
HL = pointer to the byte after the end of the string (&0000-&FFFF).
Jump out conditions: The string has been edited by the user.
                                   A = &03 => Editing ended through key ESC!
                                   A = &0E => Editing ended normally.
Manipulated: AF, BC, DE, HL, BC', DE', HL', IX, IY, PIO and PSG.
Description: With STED you can edit or create any MODE 2 text string. The string length only depends on the format of the screen. Before you call this OS function you load register DE with the address of the first byte of the string. And register HL must point to the memory address of the byte which follows the last byte of the string. That means that the length of the string is HL - DE.
Further you must tell STED which characters are allowed to be entered. Therefore register B has to be loaded with the number of the ASCII char which follows the highest allowed ASCII char. And register C must contain the ASCII value of the first allowed char.
Example: B = &80 and C = &20 =--> You can enter all ASCII characters in the region between &20 and &7F. (&20 and &7F inclusive).
While editing you can move the cursor with the arrow keys and the DEL key. To finish editing you just have to press RETURN or ENTER.
But you can stop editing through ESC too.
When the OS function jumps back register A contains a value which informs you which key has ended editing. If it is &0E, then RETURN was used, else if A contains &03 then ESC has terminated editing. The following lines could make it more clear:
Example:
LD   BC,&8020          ;chars from &20 to &7F are legal
LD   DE,&8400          ;Text string starts at address &8400
LD   HL,&8420 + 1      ;and ends at address &8420 in RAM
CALL STED              ;edit string
RRCA                   ;move bit 0 into the Carry flag
JR   C,ESC             ;If bit 0 is 1, editing was terminated using ESC
Attention: The string is not allowed to be longer than the maximal displayable number of characters (that depends on the screen format). If you have set your screen format (Mode 2 !!!) to 64 columns and 32 lines for example, the string is not allowed to be longer than nearly 2 KB. STED only works in screen mode 2. And call STED only from an address between &8000 and &BFF0 - or consider banked in RAMs! When calling this OS function register HL must be bigger than DE.
http://futureos.de --> Get the revolutionary FutureOS (Update: 2024.10.27)
http://futureos.cpc-live.com/files/LambdaSpeak_RSX_by_TFM.zip --> Get the RSX-ROM for LambdaSpeak :-) (Updated: 2021.12.26)

Bread80

The INPUT command ultimately uses the line editor built into the firmware, and this has no way to limit the length.

I'd suggest using INKEY$ to roll your own.
INKEY$ returns ascii 127 for delete, ascii 13 return. Ignore anything else below ascii 32.
You'll need to maintain your own cursor position and update it on screen (LOCATE).
Either pad a string to 12 spaces at the start (and trim trailing spaces at the end (if you care)) and use MID$ to replace characters or create an array of twelve integers

i.e MID$(s$,index,1)=char$

lightforce6128

Quote from: McArti0 on 06:05, 30 September 24
Quote from: retro space on 05:50, 30 September 24Does it take character position numbers as units for size?
No. Its coordinates.
I did a small test: the values 2,12,3,3 will create a window at character row 3 (counted starting from 1), starting in character column 2 (also counted from 1), and ending in character column 12. So the window will have a height of 1 character and a width of 11 (12-2+1) characters.

10 WINDOW #1,2,12,3,3
20 PAPER #1,3   ' define the paper for this window
30 CLS #1       ' fill the window with the defined paper (red)
40 INPUT #1,"",a$

The default window is #0 which covers the full screen and therefore can make use of hardware scrolling. Smaller windows can only use slower software scrolling. The command 'WINDOW SWAP windowA,windowB' (without the hash) allows to swap window numbers, e.g. to make another window the new default what simplifies writing down the program (you do not have to mention the window number everywhere).

DRobert

Thank you so much to all who have taken time to reply and your efforts are all appreciated.  

"Jean Marie" with the 'LEFT$ instruction' was the most simple option to my mind so I tried it and it worked perfectly within my program.  Result -  for which Im very hjappy.

Thanks once again to all. 

DRobert

Sorry but I was a bit premature with my celebrations regarding my troublesome league table.   

The above suggestion worked well by restricting the length of a team name to just 12 characters / letters and therefore when it came to printing the teams AND number of points the 'points' were not nudged a few spaces to the right due to excess length of team name which had been a problem previously.

In my trial run I had only tried team names with 12 chatacters or more which was fine.

However when I came to INPUT a shorter team name (ie Chelsea which has just 7 characters) the team prints fine but drags the 'points' total for that one team 5 spaces to the left of the other teams' points.   Ive tried pressing the space bar an extra 5 times but when it comes to the display / Print of the table the space bar taps are not recognized.   Ive INPUT the 'full stop' key 5 times which then works fine but of course it makes an otherwise professional looking table look unsightly. 

Ive tried creating separate windows to dispaly the teams and the points but the line of code that generates the league table (ie team names and points are directly correslated) cannot be split unless someone can suggest a way of simple re-coding.

The appropriate line of code for printing the league table is as follows - if it makes any sense to anyone?

LOCATE 63,p%+3;PRINT tn$(p%); "   "; pts%(p%)

As I am a rank amateur at this there is probably a simple way forward but Im struggling to see it at the moment. 

As always any help is appreciated.

Jean-Marie

You want the points to be vertically aligned, regardless of the length of the strings.
In that case, you can use the TAB(x) instruction, where x will be the coordinate of the points.

10 MODE 2
20 A$="THIS IS A LONG STRING"
30 B$="SHORT"
40 A=123
50 B=99
60 A$=LEFT$(A$,12)  'TRUNCATE LONG STRING
70 REM DISPLAY STRINGS & points
80 LOCATE 63,1:PRINT A$;TAB(76);A
90 LOCATE 63,2:PRINT B$;TAB(76);B


Jean-Marie

You should also format the value of the points, with the USING instruction. Otherwise, it will display a blank character before the value.
Now, the points stick to the right border.
10 MODE 2:DEFINT a-z
20 A$="THIS IS A LONG STRING"
30 B$="SHORT"
40 A=12
50 B=99
60 A$=LEFT$(A$,12)  'TRUNCATE LONG STRING
70 REM DISPLAY STRINGS & points
80 LOCATE 63,1:PRINT A$;TAB(79);USING"##";A
90 LOCATE 63,2:PRINT B$;TAB(79);USING"##";B


McArti0

Quote from: DRobert on 23:41, 02 October 24LOCATE 63,p%+3;PRINT tn$(p%); "  "; pts%(p%)

LOCATE 63,p%+3;PRINT tn$(p%);  SPACE$( 1+7-  LEN( tn$(p%)  )  ) ; pts%(p%)
CPC 6128, Whole 6128 and Only 6128, with .....
NewPAL v3 for use all 128kB RAM by CRTC as VRAM
One chip drver for 512kB extRAM 6128
TYPICAL :) TV Funai 22FL532/10 with VGA-RGB-in.

DRobert

Thank you to Jean-Marie and McArtiO for excellent suggestions which have helped to build my knowledge and make progress with my game.   

McArti0

Quote from: McArti0 on 06:05, 03 October 24
Quote from: DRobert on 23:41, 02 October 24LOCATE 63,p%+3;PRINT tn$(p%); "  "; pts%(p%)

LOCATE 63,p%+3;PRINT tn$(p%);  SPACE$( 1+7-  LEN( tn$(p%)  )  ) ; pts%(p%)

ZONE 7
LOCATE 63,p%+3;PRINT tn$(p%) , pts%(p%)
CPC 6128, Whole 6128 and Only 6128, with .....
NewPAL v3 for use all 128kB RAM by CRTC as VRAM
One chip drver for 512kB extRAM 6128
TYPICAL :) TV Funai 22FL532/10 with VGA-RGB-in.

Powered by SMFPacks Menu Editor Mod