Getting unsigned short value from user input with ZX80

ZX80, ZX 81, ZX Spectrum, TS2068 and other clones
Post Reply
ShaunB
Member
Posts: 41
Joined: Mon Jan 02, 2012 6:13 pm

Getting unsigned short value from user input with ZX80

Post by ShaunB »

Hi,

I'm able to use the gets(_stringBuffer); command to get keyboard inputs and pass them to a string buffer, for instance, if I was only interested in the first character and actions, I use it like this:

https://github.com/sbebbers/sunday-leag ... ain.c#L352

But is there a way to get numeric (integer) inputs, so that I don't have to iterate over each character position in the buffer and work out the value manually?

I would imagine geti(_integer); or something? Please advise.

Regards,

Shaun.
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

Post by alvin »

you can do something like this:

Code: Select all

#include <stdlib.h>

prompt("choose your option", ++y);
gets(_strBuffer);                
y = atoi(_strBuffer);
atoi will skip leading whitespace, return 0 if the first non-ws char is not a digit, and will stop scanning digits once it encounters a non-digit in the string. If you need more control over detecting errors in particular you can look at strtol or strtoul.

You can also use scanf as in scanf("%u", &y) -- y is an int. But scanf has some gotchas if you're not familiar with it already. For example, gets will read characters up to the end of a line ('\n', enter is pressed) whereas scanf only reads characters as needed so after the scanf here you haven't reached the point where the user has pressed enter yet. So usually you would follow with another scanf that would skip chars up to the \n. There's also a distinction between console and regular io in the classic library so I'm not sure if you get line editing with scanf, probably not. So it's probably best to use gets/fgets_cons to get a line as that will do line editing for sure. Once the line is in a buffer, you can also use sscanf to scan more compilcated input.
stefano
Well known member
Posts: 1632
Joined: Mon Jul 16, 2007 7:39 pm

Post by stefano »

the graphics/clock.c demo shows two different ways to do it
Post Reply