Next BASIC

The Speccy's spritely young offspring. Discuss everything from FPGA to ZX
llewelyn
Manic Miner
Posts: 205
Joined: Thu Feb 22, 2018 3:27 pm
Location: virginias eastern shore
Contact:

Re: Next BASIC

Post by llewelyn »

Thanks +3code!
It sort of worked because now the error cursor is under the zero of 20!
Ah ha! It did work after all. I had deleted the GOTO and tried running it as IF/THEN 20 and it didnt like that but as soon as I added that GOTO it worked

well sort of..trouble is it now loops back to 20 regardless of whether the input is lesser or greater than 6
dammit
Back to the manual
Alcoholics Anonymous
Microbot
Posts: 194
Joined: Mon Oct 08, 2018 3:36 am

Re: Next BASIC

Post by Alcoholics Anonymous »

Keep in mind that if it's a string, the comparisons are alphabetical order. If it's a number, the comparisons are numerical.
PeterJ wrote: Thu Sep 16, 2021 9:27 am I hope Neil completes his tutorial. I know he got COVID late last year. I've not seen him on.FB recently, but he did acknowledge one of my messages.
I'm sure he will return. He seems to be into his amateur astronomy hobby at the moment :)
llewelyn
Manic Miner
Posts: 205
Joined: Thu Feb 22, 2018 3:27 pm
Location: virginias eastern shore
Contact:

Re: Next BASIC

Post by llewelyn »

Alcoholics Anonymous » Mon Sep 20, 2021 1:53 pm
Keep in mind that if it's a string, the comparisons are alphabetical order. If it's a number, the comparisons are numerical.

I don't follow you AA. How can a string variable (typed in name) use alphabetical order as a way to compare with something?
All I wanted to do was limit the input to - say a dozen - characters long and perhaps repeat the 'type in your name' if there had been no response. Now I come to think of it, the input should also be rejected if it was numeric?

I'm trying to make the routine idiot proof but perhaps I bit off more than I can chew. A helpful manual would be useful.
+3code

Re: Next BASIC

Post by +3code »

llewelyn wrote: Mon Sep 20, 2021 10:47 pm I don't follow you AA. How can a string variable (typed in name) use alphabetical order as a way to compare with something?
Take a look at the Spectrum character set (in the Spectrum +2A Manual it's in Chapter 8, section 28): letter A is CHR$(41), letter B is CHR$(42), so "B">"A".
+3code

Re: Next BASIC

Post by +3code »

llewelyn wrote: Mon Sep 20, 2021 10:47 pm All I wanted to do was limit the input to - say a dozen - characters long and perhaps repeat the 'type in your name' if there had been no response.
You need the LEN function (in the Spectrum +2A Manual it's in Chapter 8, section 9):

IF LEN(N$)>12 THEN blablabla
dfzx
Manic Miner
Posts: 684
Joined: Mon Nov 13, 2017 6:55 pm
Location: New Forest, UK
Contact:

Re: Next BASIC

Post by dfzx »

llewelyn wrote: Mon Sep 20, 2021 10:47 pm How can a string variable (typed in name) use alphabetical order as a way to compare with something?
Simplifying somewhat, the "alphabetical" comparison works by comparing the letter's positions in the alphabet. So "A" is less than "B" because A is closer to the start of the alphabet. Longer strings are compared letter by letter until a decision can be made, so "AAQ" is less than "AAZ" (the As match, but the Q is less than Z).

You need to be careful if you store numbers as strings. "100" is less than "2" because the "1" is less than the "2", and the comparison stops at the end of the shortest string. In this case you should store the second string as "002", in which case it will give the answer you'd expect. Better, don't store numbers as strings if you want to compare numbers.

BTW, the "alphabet" I'm referring to is properly called "the ASCII table", which is like the English language alphabet but also contains the digits and punctuation marks.
Derek Fountain, author of the ZX Spectrum C Programmer's Getting Started Guide and various open source games, hardware and other projects, including an IF1 and ZX Microdrive emulator.
+3code

Re: Next BASIC

Post by +3code »

+3code wrote: Tue Sep 21, 2021 7:49 am ... letter A is CHR$(41), letter B is CHR$(42), so "B">"A".
Sorry, I mean "A" is 65 and "B" is 66 (decimal). 41 and 42 are the hexadecimal values.
+3code

Re: Next BASIC

Post by +3code »

Code: Select all

31 IF LEN(N$)=0 THEN PRINT "PLEASE, TYPE SOMETHING" : PAUSE 0 : GOTO 20
32 IF LEN(N$)>=12 THEN PRINT "PLEASE, USE A SORTER NAME" : PAUSE 0 : GOTO 20
AndyC
Dynamite Dan
Posts: 1416
Joined: Mon Nov 13, 2017 5:12 am

Re: Next BASIC

Post by AndyC »

"Alphabetical" is a very misleading way to describe it, since it most definitely isn't. Strings are ordered by character code of consecutive characters.

In ASCII, capital letters all come first so B<a, for example, which can be very confusing if you expect something strictly alphabetic.
Post Reply