*** Rude topposting corrected ***
SSM wrote:
"GMM50" <ge***********@att.net> wrote in message
I've got a project that has 100 settings. In C I've defined
these settings in an array:
#define SETTING_MAX 100
int Setting[SETTING_MAX];
The user refers to them as setting[1] through setting[100].
Internally I refer to them as setting[0] through setting[99].
Initially I was hidiing the conversion from the user; that is,
taking the user input and subtracting one from it. But this is
driving me nutty. There are a dozen places where I apply this
through out the project not just one array.
For size constraints I can not use an array from 0 to 100.
Has anyone come up with a good solution to this issue.
If you prefer the solution below which overcomes complexity at the
loss of 1 byte per array, then you can do the following:
#define SETTING_MAX 101
Then you can use array index as input by the user which is 1 to 100.
But this comes with a loss of 1 byte per array which is Setting[0].
If you had refrained from top-posting you would have seen that the
OP specifically excluded this solution.
For user interaction, the OP could have a single pair of routines:
int getindex(FILE *f) { }
int putindex(int ix, FILE *f) { }
which can encapsulate all of text/binary conversion, range
checking, and add/subtract 1. Internal to get/putindex the value 0
could mean "not entered", while outside that would be represented
by -1. If you prevent getindex from ever returning out of range,
much will be simplified.
Another possible version of getindex could supply an optional
prompt, and assume the use of stdin/stdout pair. This may be
easier for input error reporting:
int getindex(char *prompt) { }
int putindex(int ix) {} /* Assumed to stdout */
Now the program internals can universally assume C usage. Another
possibility is to switch to Pascal or Ada, where such nuisances do
not arise.
--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!