Center (centre) and right justify.

SYS 64738
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Center (centre) and right justify.

Post by Shaun_B »

This is a really quick example of centering (centring) text and right-justify:

Code: Select all

0 print "{clear}"
1 let c = 48
2 for i = 0 to 39
3 poke 1024+i, c
4 let c = c+1
5 if c > 57 then let c = 48
6 next i
7 let m$ = "donkeysoft mmxiv"
8 let c = 1
9 gosub 100
10 let m$ = "right justify"
11 let c = 0
12 gosub 100
13 end
100 let t = 40 - len( m$ )
101 if c then let t = t * 0.5
102 print tab( t ); m$
103 return
I think the centering/centring thing would be most useful. The let t = t*0.5 is equivalent to let t=t/2 but using multiplication to divide in such a way reduces rounding errors - or at least it does with modern programming languages such as JavaScript and PHP: Hypertext Preprocessor. It's probably not relevant for such a trivial example, it's just the way I'd do things at work across languages and such as I usually need consistency.

Setting the variable c to 1 or more will center/centre the text, and setting it to zero will right-justify it. Obviously, it'll only work with strings that are the width of the screen or less.

Regards,

Shaun.
Last edited by Shaun_B on Mon Jun 09, 2014 11:08 pm, edited 1 time in total.


BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
satpro

Re: Center (centre) and right justify.

Post by satpro »

Shaun,
I pasted the code into VICE and keep getting a bad subscript error in line 102. I don't know enough about BASIC to correct it (or even if it is an error -- it could be the 'paste' which is giving me grief).

Could you upload a .prg to the post?

Also, I have some good text-positioning code in ML that I'll post.
User avatar
buzbard
Member
Member
Posts: 24
Joined: Sun Apr 13, 2014 9:06 pm
Location: Washington State
Contact:

Re: Center (centre) and right justify.

Post by buzbard »

Just remove the space between "tab" and "(".
BASIC thinks it's a array variable that way instead of a function.
Ray...
It's Ok if you don't agree with me, I can't force you to be right.
satpro

Re: Center (centre) and right justify.

Post by satpro »

:oops: Oh, man.... Thanks. :)
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Center (centre) and right justify.

Post by Shaun_B »

Oops, sorry. Someone commented about posting clean listings and so I thought I'd "clean" it without realising that it could cause problems. Glad it's sorted now.

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
ajordison
Developer
Developer
Posts: 59
Joined: Wed Apr 16, 2014 1:28 pm
Location: Hartlepool, UK
Contact:

Re: Center (centre) and right justify.

Post by ajordison »

satpro wrote:I pasted the code into VICE...
Shame on you ;)
Try out CBM prg Studio for all your Commodore development needs!
Stay a while...stay for erm quite a while!
Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 6 guests